-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a setup to unit-test single components, especially fields
- Loading branch information
1 parent
cf2cac8
commit ba7d81f
Showing
6 changed files
with
760 additions
and
3 deletions.
There are no files selected for viewing
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,64 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import {render} from '@testing-library/svelte' | ||
import '@testing-library/jest-dom'; | ||
import { testOptions } from '$lib/TestOptions'; | ||
import { | ||
CallbackStateProcessor, | ||
CallbackStateProvider, | ||
Columns, | ||
CrudDefinition, | ||
DashboardDefinition, | ||
initLocale, | ||
TextField, | ||
View | ||
} from "$lib"; | ||
import ComponentToTest from "./Columns.svelte"; | ||
import TextComponent from "../ViewFieldsComponents/DefaultField.svelte"; | ||
|
||
describe( | ||
'Columns component', | ||
() => { | ||
it('can instantiate the component', async () => { | ||
const props = mockComponentProps(new Columns('columns', 'Columns label', [ | ||
{ | ||
name: 'column_1', | ||
label: 'Column 1', | ||
fields: [new TextField('text', 'Text field')] | ||
}, | ||
])); | ||
|
||
const rendered = render(ComponentToTest, props); | ||
|
||
const h2 = rendered.container.querySelector('h2'); | ||
expect(h2).toBeDefined(); | ||
expect(h2?.innerHTML).toStrictEqual('Column 1'); | ||
}); | ||
}, | ||
testOptions, | ||
); | ||
|
||
function mockComponentProps(field: Columns) { | ||
const dashboard = new DashboardDefinition({ | ||
adminConfig: {}, | ||
cruds: [ | ||
new CrudDefinition<string>({ | ||
name: 'test_field', | ||
label: { singular: 'Test Field', plural: 'Test Fields' }, | ||
operations: [new View([field])], | ||
stateProvider: new CallbackStateProvider<string>(() => Promise.resolve(null)), | ||
stateProcessor: new CallbackStateProcessor<string>(() => {}) | ||
}), | ||
] | ||
}); | ||
|
||
initLocale('fr'); | ||
|
||
return { | ||
FieldComponent: TextComponent, | ||
field: field, | ||
operation: (dashboard.cruds[0] as CrudDefinition<string>).options.operations[0], | ||
entityObject: {test_field: 'default_value'}, | ||
value: 'default_value', | ||
theme: dashboard.adminConfig.theme, | ||
}; | ||
} |
63 changes: 63 additions & 0 deletions
63
src/lib/themes/carbon/ViewFieldsComponents/ArrayField.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,63 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import {render} from '@testing-library/svelte' | ||
import '@testing-library/jest-dom'; | ||
import { testOptions } from '$lib/TestOptions'; | ||
import { | ||
CallbackStateProcessor, | ||
CallbackStateProvider, | ||
ArrayField, | ||
CrudDefinition, | ||
DashboardDefinition, | ||
initLocale, | ||
TextField, | ||
View | ||
} from "$lib"; | ||
import ComponentToTest from "./ArrayField.svelte"; | ||
|
||
describe( | ||
'ArrayField component', | ||
() => { | ||
it('can instantiate the component', async () => { | ||
const props = mockComponentProps(new ArrayField('array', 'Array label', new TextField('text', 'Text field'))); | ||
|
||
const rendered = render(ComponentToTest, props); | ||
|
||
const label = rendered.container.querySelector('div > strong'); | ||
expect(label).toBeDefined(); | ||
expect(label?.innerHTML).toStrictEqual('Text field'); | ||
const valueElement = label?.parentElement?.nextElementSibling; | ||
console.info('+=====================+'); | ||
expect(valueElement).toBeDefined(); | ||
expect(valueElement?.childNodes).toBeDefined(); | ||
expect(valueElement?.childNodes[0]).toBeInstanceOf(Text); | ||
const textNode: Text = valueElement?.childNodes[0] as Text; | ||
expect(textNode.wholeText).toStrictEqual('default_value'); | ||
}); | ||
}, | ||
testOptions, | ||
); | ||
|
||
function mockComponentProps(field: ArrayField<TextField>) { | ||
const dashboard = new DashboardDefinition({ | ||
adminConfig: {}, | ||
cruds: [ | ||
new CrudDefinition<string>({ | ||
name: 'test_field', | ||
label: { singular: 'Test Field', plural: 'Test Fields' }, | ||
operations: [new View([field])], | ||
stateProvider: new CallbackStateProvider<string>(() => Promise.resolve(null)), | ||
stateProcessor: new CallbackStateProcessor<string>(() => {}) | ||
}), | ||
] | ||
}); | ||
|
||
initLocale('fr'); | ||
|
||
return { | ||
field: field, | ||
value: ['default_value'], | ||
operation: (dashboard.cruds[0] as CrudDefinition<string>).options.operations[0], | ||
entityObject: {test_field: ['default_value']}, | ||
theme: dashboard.adminConfig.theme, | ||
}; | ||
} |
33 changes: 33 additions & 0 deletions
33
src/lib/themes/carbon/ViewFieldsComponents/CheckboxField.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,33 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import {render} from '@testing-library/svelte' | ||
import '@testing-library/jest-dom'; | ||
import { testOptions } from '$lib/TestOptions'; | ||
import ComponentToTest from "./CheckboxField.svelte"; | ||
|
||
describe( | ||
'CheckboxField component', | ||
() => { | ||
it('can instantiate the component with value "true"', async () => { | ||
const rendered = render(ComponentToTest, { | ||
value: true, | ||
}); | ||
|
||
const svg = rendered.container.querySelector('svg') as SVGSVGElement; | ||
expect(svg).toBeDefined(); | ||
expect(svg).toBeInstanceOf(SVGSVGElement); | ||
expect(svg.style.color).toBe('rgb(0, 170, 0)') | ||
}); | ||
|
||
it('can instantiate the component with value "false"', async () => { | ||
const rendered = render(ComponentToTest, { | ||
value: false, | ||
}); | ||
|
||
const svg = rendered.container.querySelector('svg') as SVGSVGElement; | ||
expect(svg).toBeDefined(); | ||
expect(svg).toBeInstanceOf(SVGSVGElement); | ||
expect(svg.style.color).toBe('rgb(170, 0, 0)') | ||
}); | ||
}, | ||
testOptions, | ||
); |
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
Oops, something went wrong.