Skip to content

Commit 9313019

Browse files
committed
LITE-27142 ActivateDialog and DeleteDialog unit tests
1 parent d92fdab commit 9313019

File tree

4 files changed

+393
-3
lines changed

4 files changed

+393
-3
lines changed
Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
import ActivateDialog from './ActivateDialog.vue';
2+
3+
import databases from '~api/databases';
4+
5+
6+
jest.mock('~api/databases', () => ({
7+
activate: jest.fn((id, obj) => ({ id, ...obj })),
8+
}));
9+
10+
describe('ActivateDialog', () => {
11+
let cmp;
12+
let context;
13+
14+
beforeEach(() => {
15+
cmp = ActivateDialog;
16+
});
17+
18+
describe('#data', () => {
19+
it('should provide initial data', () => {
20+
expect(cmp.data()).toEqual({
21+
dialogOpened: false,
22+
saving: false,
23+
form: {
24+
credentials: {
25+
name: '',
26+
host: '',
27+
username: '',
28+
password: '',
29+
},
30+
workload: 'small',
31+
},
32+
});
33+
});
34+
});
35+
36+
describe('#computed', () => {
37+
describe('#allowSaving', () => {
38+
it.each([
39+
[
40+
true,
41+
{
42+
credentials: {
43+
username: 'username',
44+
password: 'password',
45+
host: 'host',
46+
},
47+
},
48+
],
49+
[
50+
false,
51+
{
52+
credentials: {
53+
username: '',
54+
password: '',
55+
host: '',
56+
name: 'DB',
57+
},
58+
},
59+
],
60+
])('returns %s', (expected, form) => {
61+
expect(cmp.computed.allowSaving({ form })).toBe(expected);
62+
});
63+
});
64+
});
65+
66+
describe('#methods', () => {
67+
describe('#close()', () => {
68+
beforeEach(() => {
69+
context = {
70+
dialogOpened: true,
71+
form: {},
72+
$emit: jest.fn(),
73+
};
74+
75+
cmp.methods.close.call(context);
76+
});
77+
78+
it('marks dialog as closed', () => {
79+
expect(context.dialogOpened).toBe(false);
80+
});
81+
82+
it('emits `closed` signal', () => {
83+
expect(context.$emit).toHaveBeenCalledWith('closed');
84+
});
85+
86+
it('reinitializes the form', () => {
87+
expect(context.form).toEqual({
88+
credentials: {
89+
name: '',
90+
host: '',
91+
username: '',
92+
password: '',
93+
},
94+
workload: 'small',
95+
});
96+
});
97+
});
98+
99+
describe('#save()', () => {
100+
beforeEach(async () => {
101+
context = {
102+
$emit: jest.fn(),
103+
close: jest.fn(),
104+
saving: true,
105+
106+
item: {
107+
id: 'DB-123',
108+
},
109+
110+
form: {
111+
id: '...',
112+
name: 'some DB',
113+
workload: 'large',
114+
credentials: { 1: 2 },
115+
},
116+
};
117+
118+
await cmp.methods.save.call(context);
119+
});
120+
121+
it('should call activate api', () => {
122+
expect(databases.activate).toHaveBeenCalledWith(
123+
'DB-123',
124+
{
125+
workload: 'large',
126+
credentials: { 1: 2 },
127+
},
128+
);
129+
});
130+
131+
it('should emit `saved` event', () => {
132+
expect(context.$emit).toHaveBeenCalledWith('saved');
133+
});
134+
135+
it('should call #close', () => {
136+
expect(context.close).toHaveBeenCalled();
137+
});
138+
139+
it('should set saving to false', () => {
140+
expect(context.saving).toBe(false);
141+
});
142+
});
143+
});
144+
145+
describe('#watch', () => {
146+
describe('#value()', () => {
147+
it.each([true, false])('should set dialogOpen value to %s', (value) => {
148+
context = {
149+
dialogOpened: false,
150+
item: {},
151+
};
152+
153+
cmp.watch.value.handler.call(context, value);
154+
155+
expect(context.dialogOpened).toBe(value);
156+
});
157+
158+
it.each([
159+
[
160+
{
161+
id: 'DB-1',
162+
workload: 'large',
163+
},
164+
{
165+
id: 'DB-1',
166+
workload: 'large',
167+
credentials: {
168+
name: '',
169+
host: '',
170+
username: '',
171+
password: '',
172+
},
173+
},
174+
],
175+
[
176+
{
177+
name: 'db',
178+
credentials: {
179+
random: 'stuff',
180+
},
181+
},
182+
{
183+
name: 'db',
184+
credentials: {
185+
random: 'stuff',
186+
},
187+
},
188+
],
189+
])('should set dialogOpen value', (item, form) => {
190+
context = {
191+
dialogOpened: false,
192+
form: null,
193+
item,
194+
};
195+
196+
cmp.watch.value.handler.call(context, true);
197+
198+
expect(context.form).toEqual(form);
199+
});
200+
});
201+
202+
describe('#dialogOpened()', () => {
203+
let setContext;
204+
let call;
205+
206+
beforeEach(() => {
207+
setContext = (value) => {
208+
context = {
209+
value,
210+
211+
$emit: jest.fn(),
212+
};
213+
};
214+
215+
call = (...args) => (cmp.watch.dialogOpened).call(context, ...args);
216+
});
217+
218+
it.each([
219+
['foo', 'bar', ['$emit', 'toHaveBeenCalledWith', ['input', 'foo']]],
220+
['foo', 'foo', ['$emit', 'not.toHaveBeenCalledWith']],
221+
])(
222+
'When argument=%j and value=%j should "%j"',
223+
(arg, value, spec) => {
224+
/* eslint-disable-next-line prefer-const */
225+
let [property, satisfies, condition = undefined] = spec;
226+
227+
setContext(value);
228+
call(arg);
229+
230+
let assertion = expect(context[property]);
231+
232+
if (/^not\./.test(satisfies)) {
233+
assertion = assertion.not;
234+
satisfies = satisfies.replace('not.', '');
235+
}
236+
237+
assertion[satisfies](...(condition || []));
238+
},
239+
);
240+
});
241+
});
242+
});

ui/app/views/ActivateDialog.vue

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ export default {
113113
}),
114114
115115
computed: {
116-
isEdit: ({ item }) => Boolean(item),
117116
allowSaving: ({ form }) => Boolean((
118117
form.credentials.username
119118
&& form.credentials.password
@@ -128,8 +127,6 @@ export default {
128127
this.dialogOpened = false;
129128
this.$emit('closed');
130129
this.form = initialForm();
131-
this.users = [];
132-
this.regions = [];
133130
},
134131
135132
async save() {

ui/app/views/DeleteDialog.spec.js

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import DeleteDialog from './DeleteDialog.vue';
2+
3+
import databases from '~api/databases';
4+
5+
6+
jest.mock('~api/databases', () => ({
7+
delete: jest.fn(),
8+
}));
9+
10+
describe('DeleteDialog', () => {
11+
let cmp;
12+
let context;
13+
14+
beforeEach(() => {
15+
cmp = DeleteDialog;
16+
});
17+
18+
describe('#data', () => {
19+
it('should provide initial data', () => {
20+
expect(cmp.data()).toEqual({
21+
dialogOpened: false,
22+
saving: false,
23+
});
24+
});
25+
});
26+
27+
describe('#methods', () => {
28+
describe('#close()', () => {
29+
beforeEach(() => {
30+
context = {
31+
dialogOpened: true,
32+
$emit: jest.fn(),
33+
};
34+
35+
cmp.methods.close.call(context);
36+
});
37+
38+
it('marks dialog as closed', () => {
39+
expect(context.dialogOpened).toBe(false);
40+
});
41+
42+
it('emits `closed` signal', () => {
43+
expect(context.$emit).toHaveBeenCalledWith('closed');
44+
});
45+
});
46+
47+
describe('#save()', () => {
48+
beforeEach(async () => {
49+
context = {
50+
$emit: jest.fn(),
51+
close: jest.fn(),
52+
saving: true,
53+
54+
item: {
55+
id: 'DB-456',
56+
},
57+
};
58+
59+
await cmp.methods.save.call(context);
60+
});
61+
62+
it('should call delete api', () => {
63+
expect(databases.delete).toHaveBeenCalledWith('DB-456');
64+
});
65+
66+
it('should emit `saved` event', () => {
67+
expect(context.$emit).toHaveBeenCalledWith('saved');
68+
});
69+
70+
it('should call #close', () => {
71+
expect(context.close).toHaveBeenCalled();
72+
});
73+
74+
it('should set saving to false', () => {
75+
expect(context.saving).toBe(false);
76+
});
77+
});
78+
});
79+
80+
describe('#watch', () => {
81+
describe('#value()', () => {
82+
it.each([true, false])('should set dialogOpen value to %s', (value) => {
83+
context = {
84+
dialogOpened: false,
85+
};
86+
87+
cmp.watch.value.handler.call(context, value);
88+
89+
expect(context.dialogOpened).toBe(value);
90+
});
91+
});
92+
93+
describe('#dialogOpened()', () => {
94+
let setContext;
95+
let call;
96+
97+
beforeEach(() => {
98+
setContext = (value) => {
99+
context = {
100+
value,
101+
102+
$emit: jest.fn(),
103+
};
104+
};
105+
106+
call = (...args) => (cmp.watch.dialogOpened).call(context, ...args);
107+
});
108+
109+
it.each([
110+
['foo', 'bar', ['$emit', 'toHaveBeenCalledWith', ['input', 'foo']]],
111+
['foo', 'foo', ['$emit', 'not.toHaveBeenCalledWith']],
112+
])(
113+
'When argument=%j and value=%j should "%j"',
114+
(arg, value, spec) => {
115+
/* eslint-disable-next-line prefer-const */
116+
let [property, satisfies, condition = undefined] = spec;
117+
118+
setContext(value);
119+
call(arg);
120+
121+
let assertion = expect(context[property]);
122+
123+
if (/^not\./.test(satisfies)) {
124+
assertion = assertion.not;
125+
satisfies = satisfies.replace('not.', '');
126+
}
127+
128+
assertion[satisfies](...(condition || []));
129+
},
130+
);
131+
});
132+
});
133+
});

0 commit comments

Comments
 (0)