|
1 | 1 | import emailjs from './index';
|
| 2 | +import { EmailJSResponseStatus } from './models/EmailJSResponseStatus'; |
2 | 3 |
|
3 |
| -// TODO create the mocks |
| 4 | +const responseWrapper = () => { |
| 5 | + return Promise.resolve( |
| 6 | + new EmailJSResponseStatus({ |
| 7 | + status: 200, |
| 8 | + responseText: 'OK', |
| 9 | + } as XMLHttpRequest), |
| 10 | + ); |
| 11 | +}; |
| 12 | + |
| 13 | +jest.mock('./api/sendPost', () => ({ |
| 14 | + sendPost: jest.fn(() => { |
| 15 | + return responseWrapper(); |
| 16 | + }), |
| 17 | +})); |
| 18 | + |
| 19 | +it('should send method and fail on the user ID', () => { |
| 20 | + expect(() => emailjs.send('default_service', 'my_test_template')).toThrow( |
| 21 | + 'The user ID is required', |
| 22 | + ); |
| 23 | +}); |
4 | 24 |
|
5 |
| -it('should send method and fail', (done) => { |
6 |
| - emailjs |
7 |
| - .send('test', 'test') |
8 |
| - .then( |
9 |
| - (resolve) => { |
10 |
| - expect(resolve).toBeUndefined(); |
11 |
| - }, |
12 |
| - (error) => { |
13 |
| - expect(error).toBeDefined(); |
14 |
| - }, |
15 |
| - ) |
16 |
| - .finally(done); |
17 |
| -}, 10000); |
| 25 | +it('should send method and fail on the service ID', () => { |
| 26 | + emailjs.init('user_LC2JWGTestKeySomething'); |
18 | 27 |
|
19 |
| -it('should init and send method successfully', (done) => { |
20 |
| - emailjs.init('user_LC2JWGNosRSeMY6HmQFUn'); |
| 28 | + expect(() => emailjs.send('', 'my_test_template')).toThrow('The service ID is required'); |
| 29 | +}); |
21 | 30 |
|
22 |
| - emailjs |
23 |
| - .send('default_service', 'my_test_template', { |
24 |
| - |
25 |
| - to_name: 'Tester', |
26 |
| - from_name: 'JEST', |
27 |
| - message_html: '<span style="color:#ff5500">Looks like this test is passed</span>', |
28 |
| - }) |
29 |
| - .then( |
30 |
| - (resolve) => { |
31 |
| - expect(resolve).toEqual({ status: 200, text: 'OK' }); |
32 |
| - }, |
33 |
| - (error) => { |
34 |
| - expect(error).toBeUndefined(); |
35 |
| - }, |
36 |
| - ) |
37 |
| - .finally(done); |
38 |
| -}, 10000); |
| 31 | +it('should send method and fail on the template ID', () => { |
| 32 | + emailjs.init('user_LC2JWGTestKeySomething'); |
| 33 | + |
| 34 | + expect(() => emailjs.send('default_service', '')).toThrow('The template ID is required'); |
| 35 | +}); |
| 36 | + |
| 37 | +it('should init and send method successfully', async () => { |
| 38 | + emailjs.init('user_LC2JWGTestKeySomething'); |
| 39 | + |
| 40 | + try { |
| 41 | + const result = await emailjs.send('default_service', 'my_test_template'); |
| 42 | + expect(result).toEqual({ status: 200, text: 'OK' }); |
| 43 | + } catch (error) { |
| 44 | + expect(error).toBeUndefined(); |
| 45 | + } |
| 46 | +}); |
39 | 47 |
|
40 |
| -it('should send method successfully', (done) => { |
41 |
| - emailjs |
42 |
| - .send( |
| 48 | +it('should send method successfully with 4 params', async () => { |
| 49 | + try { |
| 50 | + const result = await emailjs.send( |
43 | 51 | 'default_service',
|
44 | 52 | 'my_test_template',
|
45 |
| - { |
46 |
| - |
47 |
| - to_name: 'Tester', |
48 |
| - from_name: 'JEST', |
49 |
| - message_html: '<span style="color:#ff5500">Looks like this test is passed</span>', |
50 |
| - }, |
51 |
| - 'user_LC2JWGNosRSeMY6HmQFUn', |
52 |
| - ) |
53 |
| - .then( |
54 |
| - (resolve) => { |
55 |
| - expect(resolve).toEqual({ status: 200, text: 'OK' }); |
56 |
| - }, |
57 |
| - (error) => { |
58 |
| - expect(error).toBeUndefined(); |
59 |
| - }, |
60 |
| - ) |
61 |
| - .finally(done); |
62 |
| -}, 10000); |
| 53 | + {}, |
| 54 | + 'user_LC2JWGTestKeySomething', |
| 55 | + ); |
| 56 | + expect(result).toEqual({ status: 200, text: 'OK' }); |
| 57 | + } catch (error) { |
| 58 | + expect(error).toBeUndefined(); |
| 59 | + } |
| 60 | +}); |
63 | 61 |
|
64 |
| -it('should call sendForm method form element validation', () => { |
65 |
| - expect(() => emailjs.sendForm('test', 'test', 'form-not-exist')).toThrow( |
66 |
| - 'Expected the HTML form element or the style selector of form', |
| 62 | +it('should call sendForm and throw non-form element error', () => { |
| 63 | + expect(() => emailjs.sendForm('default_service', 'my_test_template', 'form-not-exist')).toThrow( |
| 64 | + 'The 3rd parameter is expected to be the HTML form element or the style selector of form', |
67 | 65 | );
|
68 | 66 | });
|
69 | 67 |
|
70 |
| -it('should call sendForm with non id selector', (done) => { |
| 68 | +it('should call sendForm with id selector', async () => { |
71 | 69 | let form: HTMLFormElement = document.createElement('form');
|
72 | 70 | form.id = 'form-id';
|
73 | 71 | document.body.appendChild(form);
|
74 | 72 |
|
75 |
| - emailjs |
76 |
| - .sendForm('test', 'test', 'form-id') |
77 |
| - .then( |
78 |
| - (resolve) => { |
79 |
| - expect(resolve).toBeUndefined(); |
80 |
| - }, |
81 |
| - (error) => { |
82 |
| - expect(error).toBeDefined(); |
83 |
| - }, |
84 |
| - ) |
85 |
| - .finally(done); |
86 |
| -}, 10000); |
87 |
| - |
88 |
| -it('should call sendForm method and fail', (done) => { |
89 |
| - let form: HTMLFormElement = document.createElement('form'); |
90 |
| - |
91 |
| - emailjs |
92 |
| - .sendForm('test', 'test', form) |
93 |
| - .then( |
94 |
| - (resolve) => { |
95 |
| - expect(resolve).toBeUndefined(); |
96 |
| - }, |
97 |
| - (error) => { |
98 |
| - expect(error).toBeDefined(); |
99 |
| - }, |
100 |
| - ) |
101 |
| - .finally(done); |
102 |
| -}, 10000); |
| 73 | + try { |
| 74 | + const result = await emailjs.sendForm( |
| 75 | + 'default_service', |
| 76 | + 'my_test_template', |
| 77 | + '#form-id', |
| 78 | + 'user_LC2JWGTestKeySomething', |
| 79 | + ); |
| 80 | + expect(result).toEqual({ status: 200, text: 'OK' }); |
| 81 | + } catch (error) { |
| 82 | + expect(error).toBeUndefined(); |
| 83 | + } |
| 84 | +}); |
103 | 85 |
|
104 |
| -it('should call sendForm method successfully', (done) => { |
| 86 | +it('should call sendForm with form element', async () => { |
105 | 87 | let form: HTMLFormElement = document.createElement('form');
|
106 |
| - let username: HTMLInputElement = document.createElement('input'); |
107 |
| - username.name = 'username'; |
108 |
| - username.value = 'JEST'; |
109 |
| - form.appendChild(username); |
110 | 88 |
|
111 |
| - emailjs |
112 |
| - .sendForm('default_service', 'my_test_template', form, 'user_LC2JWGNosRSeMY6HmQFUn') |
113 |
| - .then( |
114 |
| - (resolve) => { |
115 |
| - expect(resolve).toEqual({ status: 200, text: 'OK' }); |
116 |
| - }, |
117 |
| - (error) => { |
118 |
| - expect(error).toBeUndefined(); |
119 |
| - }, |
120 |
| - ) |
121 |
| - .finally(done); |
122 |
| -}, 10000); |
| 89 | + try { |
| 90 | + const result = await emailjs.sendForm( |
| 91 | + 'default_service', |
| 92 | + 'my_test_template', |
| 93 | + form, |
| 94 | + 'user_LC2JWGTestKeySomething', |
| 95 | + ); |
| 96 | + expect(result).toEqual({ status: 200, text: 'OK' }); |
| 97 | + } catch (error) { |
| 98 | + expect(error).toBeUndefined(); |
| 99 | + } |
| 100 | +}); |
0 commit comments