-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1599 from undb-xyz/import-template-command-handle…
…r-test
- Loading branch information
Showing
12 changed files
with
193 additions
and
31 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
packages/core/src/base/fixtures/__snapshots__/base.fixture.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`base fixture test > create test base success 1`] = ` | ||
Base { | ||
"id": BaseId { | ||
"props": { | ||
"value": "baseId", | ||
}, | ||
}, | ||
"name": BaseName { | ||
"props": { | ||
"value": "name", | ||
}, | ||
}, | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { createTestBase } from './base.fixture' | ||
|
||
describe('base fixture test', () => { | ||
test('create test base success', () => { | ||
const base = createTestBase() | ||
|
||
expect(base).toMatchSnapshot() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { BaseFactory } from '../base.factory' | ||
import type { BaseSpecification } from '../interface' | ||
import { WithBaseId, WithBaseName } from '../specifications' | ||
|
||
export const createTestBase = (...specs: BaseSpecification[]) => { | ||
let spec = WithBaseId.fromString('baseId').and(WithBaseName.fromString('name')) | ||
|
||
for (const s of specs) { | ||
spec = spec.and(s) | ||
} | ||
|
||
return BaseFactory.create(spec) | ||
} |
50 changes: 50 additions & 0 deletions
50
packages/cqrs/src/commands/import-template/import-template.command.handler.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { ClsStore, IClsService, IRecordRepository, ITableRepository, UserId } from '@undb/core' | ||
import { TemplateFactory, createTestTemplate, templateInput } from '@undb/template' | ||
import { MockProxy, mock } from 'vitest-mock-extended' | ||
import { ImportTemplateCommand } from './import-template.command' | ||
import { ImportTemplateCommandHandler } from './import-template.command.handler' | ||
import { SpyInstance } from 'vitest' | ||
|
||
describe('import template command hanlder test ', () => { | ||
let repo: MockProxy<ITableRepository> | ||
let recordRepo: MockProxy<IRecordRepository> | ||
let cls: MockProxy<IClsService<ClsStore>> | ||
let handler: ImportTemplateCommandHandler | ||
const userId: UserId = new UserId('user1') | ||
let spy: SpyInstance | ||
let tabSpy: SpyInstance | ||
const template = createTestTemplate() | ||
|
||
beforeEach(() => { | ||
repo = mock<ITableRepository>() | ||
recordRepo = mock<IRecordRepository>() | ||
cls = mock<IClsService<ClsStore>>() | ||
spy = vi.spyOn(TemplateFactory, 'fromJSON').mockReturnValue(template) | ||
handler = new ImportTemplateCommandHandler(repo, recordRepo, cls) | ||
tabSpy = vi.spyOn(template.export, 'toTables') | ||
cls.get.calledWith('user.userId').mockReturnValue(userId.value) | ||
}) | ||
|
||
test('import template success and includeRecords is false', async () => { | ||
const command = new ImportTemplateCommand({ template: templateInput }) | ||
|
||
await handler.execute(command) | ||
|
||
expect(tabSpy).toHaveBeenCalledOnce() | ||
expect(repo.insert).toHaveBeenCalledWith(tabSpy.mock.results[0].value[0].table) | ||
expect(recordRepo.insertMany).not.toHaveBeenCalled() | ||
}) | ||
|
||
test('import template success includeRecords is true', async () => { | ||
const command = new ImportTemplateCommand({ template: templateInput, includeRecords: true }) | ||
|
||
await handler.execute(command) | ||
|
||
expect(tabSpy).toHaveBeenCalledOnce() | ||
expect(repo.insert).toHaveBeenCalledWith(tabSpy.mock.results[0].value[0].table) | ||
expect(recordRepo.insertMany).toHaveBeenCalledWith( | ||
tabSpy.mock.results[0].value[0].table, | ||
tabSpy.mock.results[0].value[0].records, | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { createTestTemplate } from './template.fixture' | ||
|
||
describe('test createTestTemplate', () => { | ||
test('create default test template success', () => { | ||
const template = createTestTemplate() | ||
|
||
expect(template).toHaveProperty(['export']) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import type { ITemplateSchema } from '../template.schema' | ||
import { TemplateFactory } from '../template.factory' | ||
|
||
export const createTestTemplate = () => { | ||
return TemplateFactory.fromJSON(templateInput) | ||
} | ||
|
||
export const templateInput: ITemplateSchema = { | ||
id: 'tplt1wjru6o', | ||
name: 'testTable', | ||
export: { | ||
tables: [ | ||
{ | ||
id: 'tblsj55f6su', | ||
name: 'testTable', | ||
schema: [ | ||
{ | ||
id: 'fldn2672c7n', | ||
name: 'id', | ||
display: false, | ||
required: false, | ||
type: 'id', | ||
}, | ||
{ | ||
id: 'fld6elro1gn', | ||
name: 'name', | ||
display: true, | ||
required: false, | ||
type: 'string', | ||
}, | ||
{ | ||
id: 'fldkek4etmf', | ||
name: 'createdAt', | ||
display: false, | ||
required: false, | ||
type: 'created-at', | ||
format: 'yyyy-MM-dd', | ||
timeFormat: null, | ||
}, | ||
{ | ||
id: 'fldx2ekp10u', | ||
name: 'createdBy', | ||
display: false, | ||
required: false, | ||
type: 'created-by', | ||
}, | ||
{ | ||
id: 'fldmvgkyxkj', | ||
name: 'updatedAt', | ||
display: false, | ||
required: false, | ||
type: 'updated-at', | ||
format: 'yyyy-MM-dd', | ||
timeFormat: null, | ||
}, | ||
{ | ||
id: 'fld9v4iiuys', | ||
name: 'updatedBy', | ||
display: false, | ||
required: false, | ||
type: 'updated-by', | ||
}, | ||
], | ||
views: [ | ||
{ | ||
id: 'viwryu43tdx', | ||
name: 'testTable', | ||
showSystemFields: false, | ||
dashboard: { | ||
widgets: [], | ||
}, | ||
displayType: 'grid', | ||
fieldOptions: {}, | ||
rowHeight: 'short', | ||
}, | ||
], | ||
viewsOrder: ['viwryu43tdx'], | ||
records: [ | ||
{ | ||
id: 'recidpnmpp6', | ||
values: { | ||
fld6elro1gn: 'test data', | ||
}, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
packages/template/src/specifications/template-export.specification.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/template/src/specifications/template-id.specification.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/template/src/specifications/template-name.specification.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 0 additions & 26 deletions
26
packages/template/src/value-objects/template-export.vo.test.ts
This file was deleted.
Oops, something went wrong.