forked from mailslurp/mailslurp-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration.spec.ts
More file actions
176 lines (169 loc) · 5.99 KB
/
integration.spec.ts
File metadata and controls
176 lines (169 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import Default, { MailSlurp } from './index';
const mailslurpRequire = require('./index').MailSlurp;
const createNewEmailAddress = jest.fn();
const createInbox = jest.fn();
const sendEmailSimple = jest.fn();
const waitForLatestEmail = jest.fn();
const getEmailsPaginated = jest.fn();
const waitForNthEmail = jest.fn();
const waitForMatchingEmail = jest.fn();
const waitForEmailCount = jest.fn();
const callOptions = {
headers: {
'x-client': 'mailslurp-client-ts-js',
},
};
jest.mock('mailslurp-swagger-sdk-ts', () => {
return {
CommonActionsControllerApi: function() {
return {
createNewEmailAddress,
sendEmailSimple,
waitForLatestEmail,
waitForNthEmail,
waitForMatchingEmail,
waitForEmailCount,
};
},
EmailControllerApi: function() {
return {
getEmailsPaginated,
};
},
InboxControllerApi: function() {
return {
createInbox,
}
},
DomainControllerApi: function() {
return {}
},
AttachmentControllerApi: function() {
return {}
},
BulkActionsControllerApi: function() {
return {}
},
};
});
beforeEach(() => {
jest.clearAllMocks();
});
describe('importing client', () => {
test('that import was successful', () => {
expect(MailSlurp).not.toBeNull();
expect(Default).not.toBeNull();
});
test('client can be instantiated', () => {
const client = new MailSlurp({ apiKey: 'test' });
expect(client).not.toBeNull();
expect(client.createNewEmailAddress).not.toBeNull();
});
test('default client can be instantiated', () => {
const client = new Default({ apiKey: 'test' });
expect(client).not.toBeNull();
expect(client.createNewEmailAddress).not.toBeNull();
});
test('require client can be instantiated', () => {
const client = new mailslurpRequire({ apiKey: 'test' });
expect(client).not.toBeNull();
expect(client.createNewEmailAddress).not.toBeNull();
});
});
describe('functions are mapped correctly to common operations api', () => {
test('can create a new email address', async () => {
const client = new MailSlurp({ apiKey: 'test' });
await client.createNewEmailAddress();
expect(createNewEmailAddress).toHaveBeenCalledTimes(1);
});
test('can create a new email address with specific address', async () => {
const client = new MailSlurp({ apiKey: 'test' });
await client.createInbox('test@gmail.com');
expect(createInbox).toHaveBeenCalledWith('test@gmail.com', callOptions);
});
test('can wrap a json error', async () => {
createNewEmailAddress.mockRejectedValue({
json: jest.fn().mockReturnValue('error-json'),
});
const client = new MailSlurp({ apiKey: 'test' });
let threw = false;
try {
await client.createNewEmailAddress();
} catch (e) {
threw = true;
expect(e).toEqual('error-json');
}
expect(threw).toBeTruthy();
expect(createNewEmailAddress).toHaveBeenCalledTimes(1);
});
test('can wrap a non json error', async () => {
createNewEmailAddress.mockRejectedValue('error-text');
const client = new MailSlurp({ apiKey: 'test', attribution: 'test-attribution' });
let threw = false;
try {
await client.createNewEmailAddress();
} catch (e) {
threw = true;
expect(e).toEqual('error-text');
}
expect(threw).toBeTruthy();
expect(createNewEmailAddress).toHaveBeenCalledWith({
headers: {
'x-attribution': 'test-attribution',
"x-client": "mailslurp-client-ts-js"
},
});
});
test('can send email', async () => {
const client = new MailSlurp({ apiKey: 'test' });
const options = {
to: [''],
};
await client.sendEmailSimple(options);
expect(sendEmailSimple).toHaveBeenCalledWith(options, callOptions);
});
test('can get all emails', async () => {
const client = new MailSlurp({ apiKey: 'test' });
await client.getAllEmails();
expect(getEmailsPaginated).toHaveBeenCalledWith(undefined, undefined, callOptions);
});
test('can get all emails with pagination', async () => {
const client = new MailSlurp({ apiKey: 'test' });
await client.getAllEmails(1, 2);
expect(getEmailsPaginated).toHaveBeenCalledWith(1, 2, callOptions);
});
test('can wait for latest email', async () => {
const client = new MailSlurp({ apiKey: 'test' });
const inboxId = 'inboxId';
const timeout = 123;
await client.waitForLatestEmail(inboxId, timeout);
expect(waitForLatestEmail).toHaveBeenCalledWith(inboxId, timeout, callOptions);
});
test('can wait for nth email', async () => {
const client = new MailSlurp({ apiKey: 'test' });
const inboxId = 'inboxId';
const index = 2;
await client.waitForNthEmail(inboxId, index);
expect(waitForNthEmail).toHaveBeenCalledWith(inboxId, index, undefined, callOptions);
});
test('can wait for matching email', async () => {
const client = new MailSlurp({ apiKey: 'test', attribution: 'test-attribution' });
const options = {};
const count = 2;
const inboxId = 'inboxId';
const timeout = 123;
await client.waitForMatchingEmails(options, count, inboxId, timeout);
expect(waitForMatchingEmail).toHaveBeenCalledWith(
options,
count,
inboxId,
timeout,
{
headers: {
'x-attribution': 'test-attribution',
'x-client': 'mailslurp-client-ts-js',
},
},
);
});
});