|
1 | 1 | import test from 'node:test' |
2 | 2 | import assert from 'node:assert/strict' |
3 | | -import { createManager } from './utils.js' |
| 3 | +import { assertProjectHasImportedCategories, createManager } from './utils.js' |
4 | 4 | import { defaultConfigPath } from '../test/helpers/default-config.js' |
| 5 | +import { Reader } from 'comapeocat' |
5 | 6 |
|
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 |
21 | 9 | }) |
22 | 10 |
|
23 | 11 | test('config import - load and re-load config manually', async (t) => { |
24 | 12 | const manager = createManager('device0', t) |
25 | 13 | const project = await manager.getProject(await manager.createProject()) |
26 | 14 |
|
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()) |
31 | 17 |
|
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) |
44 | 20 |
|
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) |
65 | 24 | }) |
66 | 25 |
|
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 |
104 | 28 | }) |
105 | 29 |
|
106 | 30 | test('failing on loading a second config should not delete any data', async (t) => { |
107 | 31 | const manager = createManager('device0', t) |
108 | 32 | const projectId = await manager.createProject() |
109 | 33 | 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 |
124 | 34 |
|
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) |
130 | 40 |
|
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 | + }) |
136 | 44 |
|
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) |
142 | 47 | }) |
143 | 48 |
|
144 | 49 | test('failing on loading multiple configs in parallel', async (t) => { |
145 | 50 | const manager = createManager('device0', t) |
146 | 51 | const project = await manager.getProject(await manager.createProject()) |
147 | 52 | 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 }), |
150 | 55 | ]) |
151 | 56 | assert.equal(results[0]?.status, 'fulfilled', 'first import should work') |
152 | 57 | assert.equal(results[1]?.status, 'rejected', 'second import should fail') |
153 | 58 | }) |
| 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