import { beforeAll, afterAll, describe, expect, it, vi } from 'vitest' import { SDK, SDKCollection } from '../src/index.js' const sdk = SDK.instance sdk.backend = 'http://localhost:5000/api' sdk.accessToken = 'abc' const date = new Date(2000, 1, 1, 1) const date1 = new Date(2001, 1, 1, 1) describe('SDK', () => { beforeAll(() => { //vi.useFakeTimers() vi.setSystemTime(date) }) afterAll(() => { //vi.useRealTimers() }) describe('Initialization', () => { it('should create models', () => { const library = sdk.libraries.construct({ id: 'my-library' }) expect(JSON.stringify(library)).toEqual( JSON.stringify({ id: 'my-library' }) ) const collection = library.collections.new({ name: 'test', // not required by we still provide it createdAt: date1 }) expect(JSON.stringify(collection)).toEqual( JSON.stringify({ libraryId: 'my-library', name: 'test', createdAt: date1, id: collection.id, isDefault: false, orderIdx: null, modifiedAt: date }) ) expect(collection.id.length).toEqual(10) expect(collection.library).toEqual(library) expect(collection.export()).toEqual({ libraryId: 'my-library', id: collection.id, isDefault: false, orderIdx: null, createdAt: date1, modifiedAt: date, name: 'test' }) const component = collection.components.construct({ name: 'component', description: 'test' }) expect(component.library).toEqual(library) expect(component.collection).toEqual(collection) expect(component.libraryId).toEqual(library.id) expect(component.collectionId).toEqual(collection.id) expect(collection.id.length).toEqual(10) const version = component.versions.new({ name: 'version1', view: 'Test' }) expect(version.library).toEqual(library) expect(version.collection).toEqual(collection) expect(version.component).toEqual(component) expect(version.libraryId).toEqual(library.id) expect(version.collectionId).toEqual(collection.id) expect(version.componentId).toEqual(component.id) expect(version.id.length).toEqual(10) const datasource = library.datasources.new({ name: 'test', description: 'Nice thing', sample: {} }) expect(datasource.library).toEqual(library) expect(datasource.libraryId).toEqual(library.id) }) it('should assign nested collections', () => { const library = sdk.libraries.construct({ id: 'my-library', collections: [{ name: 'a' }, { name: 'b' }] }) expect(library.collections.length).toEqual(2) expect(library.collections.export()).toEqual([ { libraryId: 'my-library', name: 'a' }, { libraryId: 'my-library', name: 'b' } ]) const library2 = sdk.libraries.construct({ id: 'my-library', collections: [{ name: 'a' }, { name: 'b' }] }) expect(library2.collections instanceof SDKCollection).toEqual(true) expect(library2.collections.setItems([{ name: 'a', components: [{ name: 'c1' }] }])) expect(library2.collections[0].components[0].name).toEqual('c1') expect(library2.collections[0].components[0].collection).toEqual(library2.collections[0]) expect(library2.collections[0].components[0].library).toEqual(library2) expect(library2.collections[0].components.getImplicitAttributes().library).toEqual(library2) library2.collections.setItems(library2.collections) expect(library2.collections[0].components.getImplicitAttributes().library).toEqual(library2) expect(library2.collections.getImplicitAttributes().library).toEqual(library2) expect(library2.collections[0].library).toEqual(library2) expect(library2.collections[0].components.length).toEqual(1) expect(library2.collections[0].components[0].name).toEqual('c1') expect(library2.collections[0].components[0].collection).toEqual(library2.collections[0]) expect(library2.collections[0].components[0].library).toEqual(library2) expect(library2.stylesheets.add({}).library).toEqual(library2) expect(library2.stylesheet.libraryId).toEqual(library2.id) }) }) })