Skip to content

Commit b3fea28

Browse files
committed
test: update to simulate click and keystroke instead of dispatch custom events
µIn order to make integration test instead of unit test
1 parent b5600d9 commit b3fea28

File tree

7 files changed

+119
-248
lines changed

7 files changed

+119
-248
lines changed

tests/unit/baseband.test.js

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -113,35 +113,18 @@ describe('BasebandRIL Plugin', () => {
113113
instance.emit('baseband', 'network operator 123456');
114114
const sendEventSpy = vi.spyOn(instance, 'sendEvent');
115115

116-
baseband.networkOperatorMMC.value = '123456';
117-
baseband.networkOperatorMMC.dispatchEvent(
118-
new CustomEvent('gm-text-input-change', {detail: {value: '123456'}, bubbles: true}),
119-
);
120-
121-
baseband.networkOperatorName.value = 'value';
122-
baseband.networkOperatorName.dispatchEvent(
123-
new CustomEvent('gm-text-input-change', {detail: {value: 'value'}, bubbles: true}),
124-
);
125-
126-
baseband.simOperatorMMC.value = '123456';
127-
baseband.simOperatorMMC.dispatchEvent(
128-
new CustomEvent('gm-text-input-change', {detail: {value: '123456'}, bubbles: true}),
129-
);
130-
131-
baseband.simOperatorName.value = 'value';
132-
baseband.simOperatorName.dispatchEvent(
133-
new CustomEvent('gm-text-input-change', {detail: {value: 'value'}, bubbles: true}),
134-
);
135-
136-
baseband.simMSIN.value = '012345678';
137-
baseband.simMSIN.dispatchEvent(
138-
new CustomEvent('gm-text-input-change', {detail: {value: '012345678'}, bubbles: true}),
139-
);
140-
141-
baseband.simOperatorPhoneNumber.value = '0011223344';
142-
baseband.simOperatorPhoneNumber.dispatchEvent(
143-
new CustomEvent('gm-text-input-change', {detail: {value: '0011223344'}, bubbles: true}),
144-
);
116+
const setInput = (component, value) => {
117+
const input = component.querySelector('input');
118+
input.value = value;
119+
input.dispatchEvent(new Event('input', {bubbles: true}));
120+
};
121+
122+
setInput(baseband.networkOperatorMMC, '123456');
123+
setInput(baseband.networkOperatorName, 'value');
124+
setInput(baseband.simOperatorMMC, '123456');
125+
setInput(baseband.simOperatorName, 'value');
126+
setInput(baseband.simMSIN, '012345678');
127+
setInput(baseband.simOperatorPhoneNumber, '0011223344');
145128
baseband.submitBtn.click();
146129

147130
expect(sendEventSpy).toHaveBeenCalledTimes(1);
@@ -157,9 +140,9 @@ describe('BasebandRIL Plugin', () => {
157140
],
158141
});
159142

160-
baseband.simOperatorName.value = 'value';
161-
baseband.simMSIN.value = '012345678';
162-
baseband.simOperatorPhoneNumber.value = '0011223344';
143+
setInput(baseband.simOperatorName, 'value');
144+
setInput(baseband.simMSIN, '012345678');
145+
setInput(baseband.simOperatorPhoneNumber, '0011223344');
163146
baseband.submitBtn.click();
164147
expect(sendEventSpy).toHaveBeenCalledTimes(2);
165148
expect(instance.outgoingMessages[1]).toEqual({

tests/unit/fingerprint.test.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe('FingerPrint Plugin', () => {
3737
const switchEl = document.querySelector('.autoValidationSwitch');
3838
expect(switchEl).toBeTruthy();
3939
expect(switchEl.checked).toBe(false);
40-
switchEl.dispatchEvent(new CustomEvent('gm-switch-change', {detail: {checked: true}, bubbles: true}));
40+
switchEl.click();
4141
expect(switchEl.checked).toBe(true);
4242

4343
// body
@@ -79,9 +79,7 @@ describe('FingerPrint Plugin', () => {
7979
const sendEventSpy = vi.spyOn(instance, 'sendEvent');
8080
expect(sendEventSpy).toHaveBeenCalledTimes(0);
8181
const autoValidationSwitch = document.querySelector('.autoValidationSwitch');
82-
autoValidationSwitch.dispatchEvent(
83-
new CustomEvent('gm-switch-change', {detail: {checked: true}, bubbles: true}),
84-
);
82+
autoValidationSwitch.click();
8583

8684
instance.emit('fingerprint', 'current_status scanning');
8785
instance.emit('fingerprint', 'scan start');
@@ -106,9 +104,7 @@ describe('FingerPrint Plugin', () => {
106104
.querySelector('.gm-fingerprint-button')
107105
.parentElement.classList.contains('gm-toolbar-dot-active'),
108106
).toBe(false);
109-
autoValidation.dispatchEvent(
110-
new CustomEvent('gm-switch-change', {detail: {checked: true}, bubbles: true}),
111-
);
107+
autoValidation.click();
112108
expect(
113109
document
114110
.querySelector('.gm-fingerprint-button')

tests/unit/gps.test.js

Lines changed: 22 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ describe('GPS Plugin', () => {
6969
test(`button disabled when ${field} is invalid`, () => {
7070
const fieldInput = gps.inputComponents[field];
7171
const submitButton = document.querySelector('.gm-gps-update');
72-
fieldInput.value = value;
73-
fieldInput.dispatchEvent(
74-
new CustomEvent('gm-text-input-change', {detail: {value: value}, bubbles: true}),
75-
);
72+
73+
const input = fieldInput.querySelector('input');
74+
input.value = value;
75+
input.dispatchEvent(new Event('input', {bubbles: true}));
7676
if (field !== 'accuracy' && field !== 'bearing') {
7777
expect(
7878
fieldInput.querySelector('.text-input-message').classList.contains('hidden'),
@@ -109,63 +109,13 @@ describe('GPS Plugin', () => {
109109
sendEventSpy.mockRestore();
110110
});
111111

112-
/*
113-
*test('invalid input value', () => {
114-
* gps.inputComponents.altitude.value = 'jean-michel';
115-
* gps.inputComponents.altitude.dispatchEvent(new CustomEvent('gm-text-input-change', { detail: { value: 'jean-michel' }, bubbles: true }));
116-
*
117-
* gps.inputComponents.latitude.value = 'jean-michel';
118-
* gps.inputComponents.latitude.dispatchEvent(new CustomEvent('gm-text-input-change', { detail: { value: 'jean-michel' }, bubbles: true }));
119-
*
120-
* gps.inputComponents.longitude.value = 'jean-michel';
121-
* gps.inputComponents.longitude.dispatchEvent(new CustomEvent('gm-text-input-change', { detail: { value: 'jean-michel' }, bubbles: true }));
122-
*
123-
* gps.inputComponents.accuracy.value = 'jean-michel';
124-
* gps.inputComponents.accuracy.dispatchEvent(new CustomEvent('gm-text-input-change', { detail: { value: 'jean-michel' }, bubbles: true }));
125-
*
126-
* gps.inputComponents.bearing.value = 'jean-michel';
127-
* gps.inputComponents.bearing.dispatchEvent(new CustomEvent('gm-text-input-change', { detail: { value: 'jean-michel' }, bubbles: true }));
128-
*
129-
* gps.inputComponents.speed.value = 'jean-michel';
130-
* gps.inputComponents.speed.dispatchEvent(new CustomEvent('gm-text-input-change', { detail: { value: 'jean-michel' }, bubbles: true }));
131-
* expect(document.querySelector('.gm-gps-update').disabled).toBeTruthy();
132-
*
133-
* document.querySelector('.gm-gps-update').click();
134-
*
135-
* expect(sendEventSpy).toHaveBeenCalledTimes(0);
136-
*});
137-
*/
138-
139112
test('min value', () => {
140-
gps.inputComponents.altitude.value = '-10000';
141-
gps.inputComponents.altitude.dispatchEvent(
142-
new CustomEvent('gm-text-input-change', {detail: {value: '-10000'}, bubbles: true}),
143-
);
144-
145-
gps.inputComponents.latitude.value = '-90';
146-
gps.inputComponents.latitude.dispatchEvent(
147-
new CustomEvent('gm-text-input-change', {detail: {value: '-90'}, bubbles: true}),
148-
);
149-
150-
gps.inputComponents.longitude.value = '-180';
151-
gps.inputComponents.longitude.dispatchEvent(
152-
new CustomEvent('gm-text-input-change', {detail: {value: '-180'}, bubbles: true}),
153-
);
154-
155-
gps.inputComponents.accuracy.value = '0';
156-
gps.inputComponents.accuracy.dispatchEvent(
157-
new CustomEvent('gm-text-input-change', {detail: {value: '0'}, bubbles: true}),
158-
);
159-
160-
gps.inputComponents.bearing.value = '0';
161-
gps.inputComponents.bearing.dispatchEvent(
162-
new CustomEvent('gm-text-input-change', {detail: {value: '0'}, bubbles: true}),
163-
);
164-
165-
gps.inputComponents.speed.value = '0';
166-
gps.inputComponents.speed.dispatchEvent(
167-
new CustomEvent('gm-text-input-change', {detail: {value: '0'}, bubbles: true}),
168-
);
113+
['altitude', 'latitude', 'longitude', 'accuracy', 'bearing', 'speed'].forEach((field, index) => {
114+
const vals = ['-10000', '-90', '-180', '0', '0', '0'];
115+
const input = gps.inputComponents[field].querySelector('input');
116+
input.value = vals[index];
117+
input.dispatchEvent(new Event('input', {bubbles: true}));
118+
});
169119
document.querySelector('.gm-gps-update').click();
170120

171121
expect(sendEventSpy).toHaveBeenCalledTimes(1);
@@ -184,35 +134,12 @@ describe('GPS Plugin', () => {
184134
});
185135

186136
test('max value', () => {
187-
gps.inputComponents.altitude.value = '10000';
188-
gps.inputComponents.altitude.dispatchEvent(
189-
new CustomEvent('gm-text-input-change', {detail: {value: '10000'}, bubbles: true}),
190-
);
191-
192-
gps.inputComponents.latitude.value = '90';
193-
gps.inputComponents.latitude.dispatchEvent(
194-
new CustomEvent('gm-text-input-change', {detail: {value: '90'}, bubbles: true}),
195-
);
196-
197-
gps.inputComponents.longitude.value = '180';
198-
gps.inputComponents.longitude.dispatchEvent(
199-
new CustomEvent('gm-text-input-change', {detail: {value: '180'}, bubbles: true}),
200-
);
201-
202-
gps.inputComponents.accuracy.value = '200';
203-
gps.inputComponents.accuracy.dispatchEvent(
204-
new CustomEvent('gm-text-input-change', {detail: {value: '200'}, bubbles: true}),
205-
);
206-
207-
gps.inputComponents.bearing.value = '360';
208-
gps.inputComponents.bearing.dispatchEvent(
209-
new CustomEvent('gm-text-input-change', {detail: {value: '360'}, bubbles: true}),
210-
);
211-
212-
gps.inputComponents.speed.value = '399.99';
213-
gps.inputComponents.speed.dispatchEvent(
214-
new CustomEvent('gm-text-input-change', {detail: {value: '399.99'}, bubbles: true}),
215-
);
137+
['altitude', 'latitude', 'longitude', 'accuracy', 'bearing', 'speed'].forEach((field, index) => {
138+
const vals = ['10000', '90', '180', '200', '360', '399.99'];
139+
const input = gps.inputComponents[field].querySelector('input');
140+
input.value = vals[index];
141+
input.dispatchEvent(new Event('input', {bubbles: true}));
142+
});
216143
document.querySelector('.gm-gps-update').click();
217144

218145
expect(sendEventSpy).toHaveBeenCalledTimes(1);
@@ -231,35 +158,12 @@ describe('GPS Plugin', () => {
231158
});
232159

233160
test('nominal value', () => {
234-
gps.inputComponents.altitude.value = '420';
235-
gps.inputComponents.altitude.dispatchEvent(
236-
new CustomEvent('gm-text-input-change', {detail: {value: '420'}, bubbles: true}),
237-
);
238-
239-
gps.inputComponents.latitude.value = '69'; // Nice
240-
gps.inputComponents.latitude.dispatchEvent(
241-
new CustomEvent('gm-text-input-change', {detail: {value: '69'}, bubbles: true}),
242-
);
243-
244-
gps.inputComponents.longitude.value = '3.14';
245-
gps.inputComponents.longitude.dispatchEvent(
246-
new CustomEvent('gm-text-input-change', {detail: {value: '3.14'}, bubbles: true}),
247-
);
248-
249-
gps.inputComponents.accuracy.value = '42';
250-
gps.inputComponents.accuracy.dispatchEvent(
251-
new CustomEvent('gm-text-input-change', {detail: {value: '42'}, bubbles: true}),
252-
);
253-
254-
gps.inputComponents.bearing.value = '13';
255-
gps.inputComponents.bearing.dispatchEvent(
256-
new CustomEvent('gm-text-input-change', {detail: {value: '13'}, bubbles: true}),
257-
);
258-
259-
gps.inputComponents.speed.value = '399';
260-
gps.inputComponents.speed.dispatchEvent(
261-
new CustomEvent('gm-text-input-change', {detail: {value: '399'}, bubbles: true}),
262-
);
161+
['altitude', 'latitude', 'longitude', 'accuracy', 'bearing', 'speed'].forEach((field, index) => {
162+
const vals = ['420', '69', '3.14', '42', '13', '399'];
163+
const input = gps.inputComponents[field].querySelector('input');
164+
input.value = vals[index];
165+
input.dispatchEvent(new Event('input', {bubbles: true}));
166+
});
263167
document.querySelector('.gm-gps-update').click();
264168

265169
expect(sendEventSpy).toHaveBeenCalledTimes(1);

tests/unit/identifiers.test.js

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -45,27 +45,23 @@ describe('Identifiers Plugin', () => {
4545
['android', 'device'].forEach(() => {
4646
test('button disabled', () => {
4747
expect(identifiers.submitBtn.disabled).toBeTruthy();
48-
identifiers.androidInput.value = 'jean-michel';
49-
identifiers.androidInput.dispatchEvent(
50-
new CustomEvent('gm-text-input-change', {detail: {value: 'jean-michel'}, bubbles: true}),
51-
);
48+
const androidInput = identifiers.androidInput.querySelector('input');
49+
androidInput.value = 'jean-michel';
50+
androidInput.dispatchEvent(new Event('input', {bubbles: true}));
5251

53-
identifiers.deviceInput.value = 'jean-michel';
54-
identifiers.deviceInput.dispatchEvent(
55-
new CustomEvent('gm-text-input-change', {detail: {value: 'jean-michel'}, bubbles: true}),
56-
);
52+
const deviceInput = identifiers.deviceInput.querySelector('input');
53+
deviceInput.value = 'jean-michel';
54+
deviceInput.dispatchEvent(new Event('input', {bubbles: true}));
5755

5856
expect(identifiers.submitBtn.disabled).toBeTruthy();
5957

60-
identifiers.androidInput.value = '0123456789abcdef';
61-
identifiers.androidInput.dispatchEvent(
62-
new CustomEvent('gm-text-input-change', {detail: {value: '0123456789abcdef'}, bubbles: true}),
63-
);
58+
const androidInput2 = identifiers.androidInput.querySelector('input');
59+
androidInput2.value = '0123456789abcdef';
60+
androidInput2.dispatchEvent(new Event('input', {bubbles: true}));
6461

65-
identifiers.deviceInput.value = '0123456789abcde';
66-
identifiers.deviceInput.dispatchEvent(
67-
new CustomEvent('gm-text-input-change', {detail: {value: '0123456789abcde'}, bubbles: true}),
68-
);
62+
const deviceInput2 = identifiers.deviceInput.querySelector('input');
63+
deviceInput2.value = '0123456789abcde';
64+
deviceInput2.dispatchEvent(new Event('input', {bubbles: true}));
6965

7066
expect(identifiers.submitBtn.disabled).toBeFalsy();
7167
});
@@ -112,55 +108,66 @@ describe('Identifiers Plugin', () => {
112108

113109
test('outgoing events', () => {
114110
const sendEventSpy = vi.spyOn(instance, 'sendEvent');
115-
116-
identifiers.androidInput.value = 'jean-michel';
117-
identifiers.deviceInput.value = 'jean-michel';
118-
identifiers.sendDataToInstance(new Event(''));
111+
const submitBtn = identifiers.submitBtn;
112+
113+
// Helper to simulate user input
114+
const setInput = (component, value) => {
115+
const input = component.querySelector('input');
116+
input.value = value;
117+
input.dispatchEvent(new Event('input', {bubbles: true}));
118+
};
119+
120+
setInput(identifiers.androidInput, 'jean-michel');
121+
setInput(identifiers.deviceInput, 'jean-michel');
122+
submitBtn.click();
119123
expect(sendEventSpy).toHaveBeenCalledTimes(0);
120124

121-
identifiers.androidInput.value = 'jean-michel';
122-
identifiers.deviceInput.value = '0123456789abcde';
123-
identifiers.sendDataToInstance(new Event(''));
124-
expect(sendEventSpy).toHaveBeenCalledTimes(1);
125+
setInput(identifiers.androidInput, 'jean-michel');
126+
setInput(identifiers.androidInput, '0123456789abcdef');
127+
setInput(identifiers.deviceInput, '0123456789abcde');
128+
submitBtn.click();
129+
expect(sendEventSpy).toHaveBeenCalledTimes(2);
125130
expect(instance.outgoingMessages[0]).toEqual({
131+
channel: 'framework',
132+
messages: ['set parameter android_id:0123456789abcdef'],
133+
});
134+
expect(instance.outgoingMessages[1]).toEqual({
126135
channel: 'settings',
127136
messages: ['set parameter device_id:0123456789abcde'],
128137
});
129138

130-
identifiers.androidInput.value = '0123456789abcdef';
131-
// input jean-michel is invalid so we keep the previous value
132-
identifiers.deviceInput.value = 'jean-michel';
133-
identifiers.sendDataToInstance(new Event(''));
134-
expect(sendEventSpy).toHaveBeenCalledTimes(3);
135-
expect(instance.outgoingMessages[1]).toEqual({
139+
setInput(identifiers.androidInput, '0123456789abcdef');
140+
141+
setInput(identifiers.deviceInput, 'jean-michel'); // Attempt invalid
142+
submitBtn.click();
143+
expect(sendEventSpy).toHaveBeenCalledTimes(4); // +2 messages (android + device old value)
144+
expect(instance.outgoingMessages[2]).toEqual({
136145
channel: 'framework',
137146
messages: ['set parameter android_id:0123456789abcdef'],
138147
});
139-
expect(instance.outgoingMessages[2]).toEqual({
148+
expect(instance.outgoingMessages[3]).toEqual({
140149
channel: 'settings',
141150
messages: ['set parameter device_id:0123456789abcde'],
142151
});
143152

144-
identifiers.androidInput.value = '0123456789abcdef';
145-
identifiers.deviceInput.value = '0123456789abcdb';
146-
identifiers.sendDataToInstance(new Event(''));
147-
expect(sendEventSpy).toHaveBeenCalledTimes(5);
148-
expect(instance.outgoingMessages[3]).toEqual({
153+
setInput(identifiers.deviceInput, '0123456789abcdb'); // Valid change
154+
submitBtn.click();
155+
expect(sendEventSpy).toHaveBeenCalledTimes(6); // +2 messages
156+
expect(instance.outgoingMessages[4]).toEqual({
149157
channel: 'framework',
150158
messages: ['set parameter android_id:0123456789abcdef'],
151159
});
152-
expect(instance.outgoingMessages[4]).toEqual({
160+
expect(instance.outgoingMessages[5]).toEqual({
153161
channel: 'settings',
154162
messages: ['set parameter device_id:0123456789abcdb'],
155163
});
156164

157-
identifiers.androidInput.value = '1234567891234@é%';
158-
identifiers.deviceInput.value = '0123456789abcde';
159-
identifiers.sendDataToInstance(new Event(''));
160-
expect(sendEventSpy).toHaveBeenCalledTimes(7);
161-
expect(instance.outgoingMessages[6]).toEqual({
165+
setInput(identifiers.androidInput, '1234567891234@é%'); // Invalid
166+
submitBtn.click();
167+
expect(sendEventSpy).toHaveBeenCalledTimes(8); // +2 messages (android old + device)
168+
expect(instance.outgoingMessages[7]).toEqual({
162169
channel: 'settings',
163-
messages: ['set parameter device_id:0123456789abcde'],
170+
messages: ['set parameter device_id:0123456789abcdb'],
164171
});
165172
});
166173
});

0 commit comments

Comments
 (0)