Skip to content

Commit 11aa523

Browse files
committed
feat(helper): fix and add tests
1 parent b6a9caa commit 11aa523

File tree

3 files changed

+79
-3
lines changed

3 files changed

+79
-3
lines changed

packages/field-plugin/helpers/vite/src/manifest.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ const validateSchema = (manifest: Manifest): void => {
5454
throw new Error(`When declared, the 'options' property should be an array`)
5555
}
5656

57+
//NOTE: accepted empty options case
58+
if (manifest.options.length === 0) {
59+
return
60+
}
61+
5762
validateOptions(manifest.options)
5863
}
5964

packages/manifest-helper/src/__tests__/manifest.test.ts

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe('manifest', () => {
3737
)
3838
})
3939

40-
it('should raises an exception when `options` is not an array', () => {
40+
it('should raise an exception when `options` is not an array', () => {
4141
vi.mocked(existsSync).mockReturnValue(true)
4242
vi.mocked(readFileSync).mockReturnValue(
4343
JSON.stringify({
@@ -50,7 +50,55 @@ describe('manifest', () => {
5050
)
5151
})
5252

53-
it('should raises an exception when any `options item` has no `name` property', () => {
53+
it('should raise an exception when `options` is not an array', () => {
54+
vi.mocked(existsSync).mockReturnValue(true)
55+
vi.mocked(readFileSync).mockReturnValue(
56+
JSON.stringify({
57+
options: {},
58+
}),
59+
)
60+
61+
expect(() => load()).toThrowErrorMatchingInlineSnapshot(
62+
`[Error: Error while loading the manifest file: When declared, the 'options' property should be an array]`,
63+
)
64+
})
65+
66+
it('should NOT raise an exception when `options` is an empty array', () => {
67+
vi.mocked(existsSync).mockReturnValue(true)
68+
vi.mocked(readFileSync).mockReturnValue(
69+
JSON.stringify({
70+
options: [],
71+
}),
72+
)
73+
74+
expect(() => load()).not.toThrowError()
75+
})
76+
77+
it('should NOT raise an exception when `options` is an undefined', () => {
78+
vi.mocked(existsSync).mockReturnValue(true)
79+
vi.mocked(readFileSync).mockReturnValue(
80+
JSON.stringify({
81+
options: undefined,
82+
}),
83+
)
84+
85+
expect(() => load()).not.toThrowError()
86+
})
87+
88+
it('should raise an exception when `options` is not an array', () => {
89+
vi.mocked(existsSync).mockReturnValue(true)
90+
vi.mocked(readFileSync).mockReturnValue(
91+
JSON.stringify({
92+
options: null,
93+
}),
94+
)
95+
96+
expect(() => load()).toThrowErrorMatchingInlineSnapshot(
97+
`[Error: Error while loading the manifest file: When declared, the 'options' property should be an array]`,
98+
)
99+
})
100+
101+
it('should raise an exception when any `options item` has no `name` property', () => {
54102
vi.mocked(existsSync).mockReturnValue(true)
55103
vi.mocked(readFileSync).mockReturnValue(
56104
JSON.stringify({
@@ -69,7 +117,7 @@ describe('manifest', () => {
69117
)
70118
})
71119

72-
it('should raises an exception when any `options item` has no `value` property', () => {
120+
it('should raise an exception when any `options item` has no `value` property', () => {
73121
vi.mocked(existsSync).mockReturnValue(true)
74122
vi.mocked(readFileSync).mockReturnValue(
75123
JSON.stringify({
@@ -88,6 +136,24 @@ describe('manifest', () => {
88136
)
89137
})
90138

139+
it('should raise an exception when any `options item` --> `value` property is not of type string', () => {
140+
vi.mocked(existsSync).mockReturnValue(true)
141+
vi.mocked(readFileSync).mockReturnValue(
142+
JSON.stringify({
143+
options: [
144+
{
145+
name: 'opt1',
146+
value: 1,
147+
},
148+
],
149+
}),
150+
)
151+
152+
expect(() => load()).toThrowErrorMatchingInlineSnapshot(
153+
`[Error: Error while loading the manifest file: Each option must be an object with string properties "name" and "value". The following values need to be corrected: \n {"name":"opt1","value":1} --> Incorrect value type. Must be of type string.]`,
154+
)
155+
})
156+
91157
it('should be valid but with no `options` defined', () => {
92158
vi.mocked(existsSync).mockReturnValue(true)
93159
vi.mocked(readFileSync).mockReturnValue('{}')

packages/manifest-helper/src/manifest.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ const validateSchema = (manifest: Manifest): void => {
5757
throw new Error(`When declared, the 'options' property should be an array`)
5858
}
5959

60+
//NOTE: accepted empty options case
61+
if (manifest.options.length === 0) {
62+
return
63+
}
64+
6065
validateOptions(manifest.options)
6166
}
6267

0 commit comments

Comments
 (0)