Skip to content

Commit 1337426

Browse files
committed
updated config-import tests
1 parent 215a2f4 commit 1337426

1 file changed

Lines changed: 36 additions & 120 deletions

File tree

test-e2e/config-import.js

Lines changed: 36 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,153 +1,69 @@
11
import test from 'node:test'
22
import assert from 'node:assert/strict'
3-
import { createManager } from './utils.js'
3+
import { assertProjectHasImportedCategories, createManager } from './utils.js'
44
import { defaultConfigPath } from '../test/helpers/default-config.js'
5+
import { Reader } from 'comapeocat'
56

6-
test(' config import - load default config when passed a path to `createProject`', async (t) => {
7-
const manager = createManager('device0', t)
8-
const project = await manager.getProject(
9-
await manager.createProject({ configPath: defaultConfigPath })
10-
)
11-
const presets = await project.preset.getMany()
12-
const fields = await project.field.getMany()
13-
const translations = await project.$translation.dataType.getMany()
14-
assert.equal(presets.length, 28, 'correct number of loaded presets')
15-
assert.equal(fields.length, 11, 'correct number of loaded fields')
16-
assert.equal(
17-
translations.length,
18-
870,
19-
'correct number of loaded translations'
20-
)
7+
test.skip('config import - load default config when passed a path to `createProject`', async () => {
8+
// This is tested in manager-basic.js
219
})
2210

2311
test('config import - load and re-load config manually', async (t) => {
2412
const manager = createManager('device0', t)
2513
const project = await manager.getProject(await manager.createProject())
2614

27-
const warnings = await project.importConfig({ configPath: defaultConfigPath })
28-
let presets = await project.preset.getMany()
29-
let fields = await project.field.getMany()
30-
let translations = await project.$translation.dataType.getMany()
15+
const reader = new Reader(defaultConfigPath)
16+
t.after(() => reader.close())
3117

32-
assert.equal(
33-
warnings.length,
34-
0,
35-
'no warnings when manually loading default config'
36-
)
37-
assert.equal(presets.length, 28, 'correct number of manually loaded presets')
38-
assert.equal(fields.length, 11, 'correct number of manually loaded fields')
39-
assert.equal(
40-
translations.length,
41-
870,
42-
'correct number of manually loaded translations'
43-
)
18+
await project.$importCategories({ filePath: defaultConfigPath })
19+
await assertProjectHasImportedCategories(project, reader)
4420

45-
// re load the config
46-
await project.importConfig({ configPath: defaultConfigPath })
47-
presets = await project.preset.getMany()
48-
fields = await project.field.getMany()
49-
translations = await project.$translation.dataType.getMany()
50-
assert.equal(
51-
presets.length,
52-
28,
53-
're-loading the same config leads to the same number of presets (since they are deleted)'
54-
)
55-
assert.equal(
56-
fields.length,
57-
11,
58-
're-loading the same config leads to the same number of fields (since they are deleted)'
59-
)
60-
assert.equal(
61-
translations.length,
62-
870,
63-
're-loading the same config leads to the same number of translations (since they are deleted)'
64-
)
21+
// re-load the config
22+
await project.$importCategories({ filePath: defaultConfigPath })
23+
await assertProjectHasImportedCategories(project, reader)
6524
})
6625

67-
test('deletion of data before loading a new config', async (t) => {
68-
const manager = createManager('device0', t)
69-
const project = await manager.getProject(await manager.createProject())
70-
71-
// load default config
72-
await project.importConfig({
73-
configPath: defaultConfigPath,
74-
})
75-
const nPresets = (await project.preset.getMany()).length
76-
const nFields = (await project.field.getMany()).length
77-
const nTranslations = (await project.$translation.dataType.getMany()).length
78-
79-
// load another config
80-
await project.importConfig({
81-
configPath: './test/fixtures/config/validConfig.zip',
82-
})
83-
84-
// load default config again
85-
await project.importConfig({
86-
configPath: defaultConfigPath,
87-
})
88-
89-
assert.equal(
90-
(await project.preset.getMany()).length,
91-
nPresets,
92-
'after loading config 1, then 2, then 1 again, number of presets should be equal'
93-
)
94-
assert.equal(
95-
(await project.field.getMany()).length,
96-
nFields,
97-
'after loading config 1, then 2, then 1 again, number of fields should be equal'
98-
)
99-
assert.equal(
100-
(await project.$translation.dataType.getMany()).length,
101-
nTranslations,
102-
'after loading config 1, then 2, then 1 again, number of translations should be equal'
103-
)
26+
test.skip('deletion of data before loading a new config', async () => {
27+
// This is tested in manager-basic tests
10428
})
10529

10630
test('failing on loading a second config should not delete any data', async (t) => {
10731
const manager = createManager('device0', t)
10832
const projectId = await manager.createProject()
10933
const project = await manager.getProject(projectId)
110-
// load default config
111-
await project.importConfig({ configPath: defaultConfigPath })
112-
113-
const nPresets = (await project.preset.getMany()).length
114-
const nFields = (await project.field.getMany()).length
115-
const nTranslations = (await project.$translation.dataType.getMany()).length
116-
117-
// load a non-existent config
118-
await project.importConfig({ configPath: 'hi' })
119-
120-
const nPresetsAfter = (await project.preset.getMany()).length
121-
const nFieldsAfter = (await project.field.getMany()).length
122-
const nTranslationsAfter = (await project.$translation.dataType.getMany())
123-
.length
12434

125-
assert.equal(
126-
nPresetsAfter,
127-
nPresets,
128-
'after failing to load a config, we should not delete any older presets'
129-
)
35+
const reader = new Reader(defaultConfigPath)
36+
t.after(() => reader.close())
37+
// load default config
38+
await project.$importCategories({ filePath: defaultConfigPath })
39+
await assertProjectHasImportedCategories(project, reader)
13040

131-
assert.equal(
132-
nFieldsAfter,
133-
nFields,
134-
'after failing to load a config, we should not delete any older fields'
135-
)
41+
await assert.rejects(() => project.$importCategories({ filePath: 'hi' }), {
42+
code: 'ENOENT',
43+
})
13644

137-
assert.equal(
138-
nTranslationsAfter,
139-
nTranslations,
140-
'after failing to load a config, we should not delete any older translations'
141-
)
45+
// check we still have the original data
46+
await assertProjectHasImportedCategories(project, reader)
14247
})
14348

14449
test('failing on loading multiple configs in parallel', async (t) => {
14550
const manager = createManager('device0', t)
14651
const project = await manager.getProject(await manager.createProject())
14752
const results = await Promise.allSettled([
148-
project.importConfig({ configPath: defaultConfigPath }),
149-
project.importConfig({ configPath: defaultConfigPath }),
53+
project.$importCategories({ filePath: defaultConfigPath }),
54+
project.$importCategories({ filePath: defaultConfigPath }),
15055
])
15156
assert.equal(results[0]?.status, 'fulfilled', 'first import should work')
15257
assert.equal(results[1]?.status, 'rejected', 'second import should fail')
15358
})
59+
60+
test('deprecated project.importConfig', async (t) => {
61+
const manager = createManager('device0', t)
62+
const project = await manager.getProject(await manager.createProject())
63+
64+
const reader = new Reader(defaultConfigPath)
65+
t.after(() => reader.close())
66+
67+
await project.importConfig({ configPath: defaultConfigPath })
68+
await assertProjectHasImportedCategories(project, reader)
69+
})

0 commit comments

Comments
 (0)