diff options
Diffstat (limited to 'packages/main/src/stores/__tests__/SharedStore.spec.ts')
-rw-r--r-- | packages/main/src/stores/__tests__/SharedStore.spec.ts | 160 |
1 files changed, 160 insertions, 0 deletions
diff --git a/packages/main/src/stores/__tests__/SharedStore.spec.ts b/packages/main/src/stores/__tests__/SharedStore.spec.ts new file mode 100644 index 0000000..3ea187c --- /dev/null +++ b/packages/main/src/stores/__tests__/SharedStore.spec.ts | |||
@@ -0,0 +1,160 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2021-2022 Kristóf Marussy <kristof@marussy.com> | ||
3 | * | ||
4 | * This file is part of Sophie. | ||
5 | * | ||
6 | * Sophie is free software: you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU Affero General Public License as | ||
8 | * published by the Free Software Foundation, version 3. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU Affero General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU Affero General Public License | ||
16 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
17 | * | ||
18 | * SPDX-License-Identifier: AGPL-3.0-only | ||
19 | */ | ||
20 | |||
21 | import type { ProfileConfig } from '../Profile'; | ||
22 | import type { ServiceConfig } from '../Service'; | ||
23 | import { Config, sharedStore, SharedStore } from '../SharedStore'; | ||
24 | |||
25 | const profileProps: ProfileConfig = { | ||
26 | name: 'Test profile', | ||
27 | }; | ||
28 | |||
29 | const serviceProps: ServiceConfig = { | ||
30 | name: 'Test service', | ||
31 | url: 'https://example.com', | ||
32 | }; | ||
33 | |||
34 | let sut: SharedStore; | ||
35 | |||
36 | beforeEach(() => { | ||
37 | sut = sharedStore.create(); | ||
38 | }); | ||
39 | |||
40 | describe('loadConfig', () => { | ||
41 | it('should load profiles with an ID', () => { | ||
42 | sut.loadConfig({ | ||
43 | profiles: [ | ||
44 | { | ||
45 | id: 'someId', | ||
46 | ...profileProps, | ||
47 | }, | ||
48 | ], | ||
49 | }); | ||
50 | expect(sut.profiles[0].id).toBe('someId'); | ||
51 | }); | ||
52 | |||
53 | it('should generate an ID for profiles without and ID', () => { | ||
54 | sut.loadConfig({ | ||
55 | profiles: [profileProps], | ||
56 | }); | ||
57 | expect(sut.profiles[0].id).toBeDefined(); | ||
58 | }); | ||
59 | |||
60 | it('should load services with an ID and a profile', () => { | ||
61 | sut.loadConfig({ | ||
62 | profiles: [ | ||
63 | { | ||
64 | id: 'someProfileId', | ||
65 | ...profileProps, | ||
66 | }, | ||
67 | ], | ||
68 | services: [ | ||
69 | { | ||
70 | id: 'someServiceId', | ||
71 | profile: 'someProfileId', | ||
72 | ...serviceProps, | ||
73 | }, | ||
74 | ], | ||
75 | }); | ||
76 | expect(sut.services[0].id).toBe('someServiceId'); | ||
77 | expect(sut.services[0].settings.profile).toBe(sut.profiles[0]); | ||
78 | }); | ||
79 | |||
80 | it('should refuse to load a profile without a name', () => { | ||
81 | expect(() => { | ||
82 | sut.loadConfig({ | ||
83 | profiles: [ | ||
84 | { | ||
85 | id: 'someProfileId', | ||
86 | ...profileProps, | ||
87 | name: undefined, | ||
88 | }, | ||
89 | ], | ||
90 | } as unknown as Config); | ||
91 | }).toThrow(); | ||
92 | expect(sut.profiles).toHaveLength(0); | ||
93 | }); | ||
94 | |||
95 | it('should load services without an ID but with a profile', () => { | ||
96 | sut.loadConfig({ | ||
97 | profiles: [ | ||
98 | { | ||
99 | id: 'someProfileId', | ||
100 | ...profileProps, | ||
101 | }, | ||
102 | ], | ||
103 | services: [ | ||
104 | { | ||
105 | profile: 'someProfileId', | ||
106 | ...serviceProps, | ||
107 | }, | ||
108 | ], | ||
109 | }); | ||
110 | expect(sut.services[0].id).toBeDefined(); | ||
111 | expect(sut.services[0].settings.profile).toBe(sut.profiles[0]); | ||
112 | }); | ||
113 | |||
114 | it('should create a profile for a service with an ID but no profile', () => { | ||
115 | sut.loadConfig({ | ||
116 | services: [ | ||
117 | { | ||
118 | id: 'someServiceId', | ||
119 | ...serviceProps, | ||
120 | }, | ||
121 | ], | ||
122 | }); | ||
123 | expect(sut.services[0].id).toBe('someServiceId'); | ||
124 | expect(sut.services[0].settings.profile).toBeDefined(); | ||
125 | expect(sut.services[0].settings.profile.settings.name).toBe( | ||
126 | serviceProps.name, | ||
127 | ); | ||
128 | }); | ||
129 | |||
130 | it('should create a profile for a service without an ID or profile', () => { | ||
131 | sut.loadConfig({ | ||
132 | services: [ | ||
133 | { | ||
134 | ...serviceProps, | ||
135 | }, | ||
136 | ], | ||
137 | }); | ||
138 | expect(sut.services[0].id).toBeDefined(); | ||
139 | expect(sut.services[0].settings.profile).toBeDefined(); | ||
140 | expect(sut.services[0].settings.profile.settings.name).toBe( | ||
141 | serviceProps.name, | ||
142 | ); | ||
143 | }); | ||
144 | |||
145 | it('should refuse to load a service without a name', () => { | ||
146 | expect(() => { | ||
147 | sut.loadConfig({ | ||
148 | services: [ | ||
149 | { | ||
150 | id: 'someServiceId', | ||
151 | ...serviceProps, | ||
152 | name: undefined, | ||
153 | }, | ||
154 | ], | ||
155 | } as unknown as Config); | ||
156 | }).toThrow(); | ||
157 | expect(sut.profiles).toHaveLength(0); | ||
158 | expect(sut.services).toHaveLength(0); | ||
159 | }); | ||
160 | }); | ||