aboutsummaryrefslogtreecommitdiffstats
path: root/packages/main/src/stores/__tests__/SharedStore.test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/main/src/stores/__tests__/SharedStore.test.ts')
-rw-r--r--packages/main/src/stores/__tests__/SharedStore.test.ts161
1 files changed, 161 insertions, 0 deletions
diff --git a/packages/main/src/stores/__tests__/SharedStore.test.ts b/packages/main/src/stores/__tests__/SharedStore.test.ts
new file mode 100644
index 0000000..78396da
--- /dev/null
+++ b/packages/main/src/stores/__tests__/SharedStore.test.ts
@@ -0,0 +1,161 @@
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
21import SharedStore from '../SharedStore.js';
22import type Config from '../config/Config.js';
23import type ProfileConfig from '../config/ProfileConfig.js';
24import type ServiceConfig from '../config/ServiceConfig.js';
25
26const profileProps: ProfileConfig = {
27 name: 'Test profile',
28};
29
30const serviceProps: ServiceConfig = {
31 name: 'Test service',
32 url: 'https://example.com',
33};
34
35let sut: SharedStore;
36
37beforeEach(() => {
38 sut = SharedStore.create();
39});
40
41describe('loadConfig', () => {
42 test('loads profiles with an ID', () => {
43 sut.loadConfig({
44 profiles: [
45 {
46 id: 'someId',
47 ...profileProps,
48 },
49 ],
50 });
51 expect(sut.profiles[0].id).toBe('someId');
52 });
53
54 test('generates an ID for profiles without and ID', () => {
55 sut.loadConfig({
56 profiles: [profileProps],
57 });
58 expect(sut.profiles[0].id).toBeDefined();
59 });
60
61 test('loads services with an ID and a profile', () => {
62 sut.loadConfig({
63 profiles: [
64 {
65 id: 'someProfileId',
66 ...profileProps,
67 },
68 ],
69 services: [
70 {
71 id: 'someServiceId',
72 profile: 'someProfileId',
73 ...serviceProps,
74 },
75 ],
76 });
77 expect(sut.services[0].id).toBe('someServiceId');
78 expect(sut.services[0].settings.profile).toBe(sut.profiles[0]);
79 });
80
81 test('refuses to load a profile without a name', () => {
82 expect(() => {
83 sut.loadConfig({
84 profiles: [
85 {
86 id: 'someProfileId',
87 ...profileProps,
88 name: undefined,
89 },
90 ],
91 } as unknown as Config);
92 }).toThrow();
93 expect(sut.profiles).toHaveLength(0);
94 });
95
96 test('loads services without an ID but with a profile', () => {
97 sut.loadConfig({
98 profiles: [
99 {
100 id: 'someProfileId',
101 ...profileProps,
102 },
103 ],
104 services: [
105 {
106 profile: 'someProfileId',
107 ...serviceProps,
108 },
109 ],
110 });
111 expect(sut.services[0].id).toBeDefined();
112 expect(sut.services[0].settings.profile).toBe(sut.profiles[0]);
113 });
114
115 test('creates a profile for a service with an ID but no profile', () => {
116 sut.loadConfig({
117 services: [
118 {
119 id: 'someServiceId',
120 ...serviceProps,
121 },
122 ],
123 });
124 expect(sut.services[0].id).toBe('someServiceId');
125 expect(sut.services[0].settings.profile).toBeDefined();
126 expect(sut.services[0].settings.profile.settings.name).toBe(
127 serviceProps.name,
128 );
129 });
130
131 test('creates a profile for a service without an ID or profile', () => {
132 sut.loadConfig({
133 services: [
134 {
135 ...serviceProps,
136 },
137 ],
138 });
139 expect(sut.services[0].id).toBeDefined();
140 expect(sut.services[0].settings.profile).toBeDefined();
141 expect(sut.services[0].settings.profile.settings.name).toBe(
142 serviceProps.name,
143 );
144 });
145
146 test('refuses to load a service without a name', () => {
147 expect(() => {
148 sut.loadConfig({
149 services: [
150 {
151 id: 'someServiceId',
152 ...serviceProps,
153 name: undefined,
154 },
155 ],
156 } as unknown as Config);
157 }).toThrow();
158 expect(sut.profiles).toHaveLength(0);
159 expect(sut.services).toHaveLength(0);
160 });
161});