Skip to content

Commit bbf35ec

Browse files
committed
add headless tests and promise cover
1 parent 05fd600 commit bbf35ec

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

src/it.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,21 @@ describe('send method', () => {
2626
expect(error).toBeUndefined();
2727
}
2828
});
29+
30+
it('should call the init and the send method successfully as promise', () => {
31+
emailjs.init({
32+
publicKey: 'C2JWGTestKeySomething',
33+
});
34+
35+
return emailjs.send('default_service', 'my_test_template').then(
36+
(result) => {
37+
expect(result).toEqual({ status: 200, text: 'OK' });
38+
},
39+
(error) => {
40+
expect(error).toBeUndefined();
41+
},
42+
);
43+
});
2944
});
3045

3146
describe('send-form method', () => {
@@ -43,4 +58,21 @@ describe('send-form method', () => {
4358
expect(error).toBeUndefined();
4459
}
4560
});
61+
62+
it('should call the init and the sendForm method successfully as promise', () => {
63+
const form: HTMLFormElement = document.createElement('form');
64+
65+
emailjs.init({
66+
publicKey: 'C2JWGTestKeySomething',
67+
});
68+
69+
return emailjs.sendForm('default_service', 'my_test_template', form).then(
70+
(result) => {
71+
expect(result).toEqual({ status: 200, text: 'OK' });
72+
},
73+
(error) => {
74+
expect(error).toBeUndefined();
75+
},
76+
);
77+
});
4678
});

src/methods/send/send.spec.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,43 @@ describe('sdk v4', () => {
5555
).toThrow('The service ID is required');
5656
});
5757

58+
it('should call the send method and fail on headless', async () => {
59+
try {
60+
const result = await send(
61+
'default_service',
62+
'my_test_template',
63+
{},
64+
{
65+
publicKey: 'C2JWGTestKeySomething',
66+
blockHeadless: true,
67+
},
68+
);
69+
expect(result).toBeUndefined();
70+
} catch (error) {
71+
expect(error).toEqual({
72+
status: 451,
73+
text: 'Unavailable For Headless Browser',
74+
});
75+
}
76+
});
77+
78+
it('should call the send method and fail on headless as promise', () => {
79+
return send('', 'my_test_template', undefined, {
80+
publicKey: 'C2JWGTestKeySomething',
81+
blockHeadless: true,
82+
}).then(
83+
(result) => {
84+
expect(result).toBeUndefined();
85+
},
86+
(error) => {
87+
expect(error).toEqual({
88+
status: 451,
89+
text: 'Unavailable For Headless Browser',
90+
});
91+
},
92+
);
93+
});
94+
5895
it('should call the send method and fail on the template ID', () => {
5996
expect(() =>
6097
send('default_service', '', undefined, {
@@ -78,4 +115,22 @@ describe('sdk v4', () => {
78115
expect(error).toBeUndefined();
79116
}
80117
});
118+
119+
it('should call the send method as promise', () => {
120+
return send(
121+
'default_service',
122+
'my_test_template',
123+
{},
124+
{
125+
publicKey: 'C2JWGTestKeySomething',
126+
},
127+
).then(
128+
(result) => {
129+
expect(result).toEqual({ status: 200, text: 'OK' });
130+
},
131+
(error) => {
132+
expect(error).toBeUndefined();
133+
},
134+
);
135+
});
81136
});

src/methods/sendForm/sendForm.spec.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,42 @@ describe('sdk v4', () => {
9090
).toThrow('The template ID is required');
9191
});
9292

93+
it('should call the sendForm and fail on headless', async () => {
94+
const form: HTMLFormElement = document.createElement('form');
95+
96+
try {
97+
const result = await sendForm('default_service', 'my_test_template', form, {
98+
publicKey: 'C2JWGTestKeySomething',
99+
blockHeadless: true,
100+
});
101+
expect(result).toBeUndefined();
102+
} catch (error) {
103+
expect(error).toEqual({
104+
status: 451,
105+
text: 'Unavailable For Headless Browser',
106+
});
107+
}
108+
});
109+
110+
it('should call the sendForm and fail on headless as promise', () => {
111+
const form: HTMLFormElement = document.createElement('form');
112+
113+
return sendForm('default_service', 'my_test_template', form, {
114+
publicKey: 'C2JWGTestKeySomething',
115+
blockHeadless: true,
116+
}).then(
117+
(result) => {
118+
expect(result).toBeUndefined();
119+
},
120+
(error) => {
121+
expect(error).toEqual({
122+
status: 451,
123+
text: 'Unavailable For Headless Browser',
124+
});
125+
},
126+
);
127+
});
128+
93129
it('should call the sendForm with id selector', async () => {
94130
const form: HTMLFormElement = document.createElement('form');
95131
form.id = 'form-id';
@@ -117,4 +153,19 @@ describe('sdk v4', () => {
117153
expect(error).toBeUndefined();
118154
}
119155
});
156+
157+
it('should call the sendForm with form element as promise', () => {
158+
const form: HTMLFormElement = document.createElement('form');
159+
160+
return sendForm('default_service', 'my_test_template', form, {
161+
publicKey: 'C2JWGTestKeySomething',
162+
}).then(
163+
(result) => {
164+
expect(result).toEqual({ status: 200, text: 'OK' });
165+
},
166+
(error) => {
167+
expect(error).toBeUndefined();
168+
},
169+
);
170+
});
120171
});

0 commit comments

Comments
 (0)