Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: test import template command handler #1599

Merged
merged 5 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
},
},
}
`;
9 changes: 9 additions & 0 deletions packages/core/src/base/fixtures/base.fixture.test.ts
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()
})
})
13 changes: 13 additions & 0 deletions packages/core/src/base/fixtures/base.fixture.ts
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)
}
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,
)
})
})
3 changes: 2 additions & 1 deletion packages/template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"clean": "rimraf dist",
"coverage": "vitest run --coverage",
"dev": "tsup --watch",
"prebuild": "pnpm run clean"
"prebuild": "pnpm run clean",
"test":"vitest run"
},
"type": "module",
"types": "dist/index.d",
Expand Down
9 changes: 9 additions & 0 deletions packages/template/src/fixtures/template.fixture.test.ts
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'])
})
})
89 changes: 89 additions & 0 deletions packages/template/src/fixtures/template.fixture.ts
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',
},
},
],
},
],
},
}
1 change: 1 addition & 0 deletions packages/template/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './template.factory.js'
export * from './template.js'
export * from './template.schema.js'
export * from './value-objects/index.js'
export * from './fixtures/template.fixture.js'
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CompositeSpecification } from '@undb/domain'
import { Ok, type Result } from 'oxide.ts'
import type { ITemplateExportSchema } from 'src/value-objects/template-export.vo.js'
import { TemplateExport } from 'src/value-objects/template-export.vo.js'
import type { ITemplateExportSchema } from '../value-objects/template-export.vo.js'
import { TemplateExport } from '../value-objects/template-export.vo.js'
import type { ITemplateVisitor } from '../interface.js'
import type { Template } from '../template.js'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CompositeSpecification } from '@undb/domain'
import { Ok, type Result } from 'oxide.ts'
import { TemplateID } from 'src/value-objects/template-id.vo.js'
import { TemplateID } from '../value-objects/template-id.vo.js'
import type { ITemplateVisitor } from '../interface.js'
import type { Template } from '../template.js'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CompositeSpecification } from '@undb/domain'
import { Ok, type Result } from 'oxide.ts'
import { TemplateName } from 'src/value-objects/template-name.vo.js'
import { TemplateName } from '../value-objects/template-name.vo.js'
import type { ITemplateVisitor } from '../interface.js'
import type { Template } from '../template.js'

Expand Down
26 changes: 0 additions & 26 deletions packages/template/src/value-objects/template-export.vo.test.ts

This file was deleted.