forked from mailslurp/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.test.ts
83 lines (81 loc) · 2.84 KB
/
example.test.ts
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
import {expect, test} from 'vitest'
import {MatchOptionFieldEnum, MatchOptionShouldEnum} from "mailslurp-client";
const YOUR_API_KEY = process.env.API_KEY;
test('can wait for email', async () => {
expect(YOUR_API_KEY).toBeDefined()
//<gen>wait_for_methods_client
const {MailSlurp} = await import("mailslurp-client");
const mailslurp = new MailSlurp({
apiKey: YOUR_API_KEY
});
const inbox = await mailslurp.createInboxWithOptions({
// expires in 5 minutes
expiresIn: 300_000
});
expect(inbox.emailAddress).toContain("@mailslurp");
//</gen>
//<gen>wait_for_methods_test_wait_for_latest
// send an email
await mailslurp.inboxController.sendEmailAndConfirm({
inboxId: inbox.id,
sendEmailOptions: {
to: [inbox.emailAddress],
subject: "First email",
}
})
// wait for the first unread email to arrive
const email = await mailslurp.waitController.waitForLatestEmail({
timeout: 120_000,
inboxId: inbox.id,
unreadOnly: true
})
expect(email.subject).toContain('First email')
//</gen>
//<gen>wait_for_methods_test_wait_for_matching
// send two emails
for (const i of [1, 2]) {
await mailslurp.inboxController.sendEmailAndConfirm({
inboxId: inbox.id,
sendEmailOptions: {
to: [inbox.emailAddress],
// send a different message each time
subject: `Match subject test-${i}`,
}
})
}
// wait for 2 emails matching the subject with a pattern
const emails = await mailslurp.waitController.waitForMatchingEmails({
inboxId: inbox.id,
timeout: 120_000,
unreadOnly: true,
count: 2,
matchOptions: {
matches: [
{
// expect subject to contain "Match subject"
value: "Match subject",
field: MatchOptionFieldEnum.SUBJECT,
should: MatchOptionShouldEnum.CONTAIN
}
]
}
})
// assert we received two emails matching the subject
expect(emails.length).toEqual(2)
// the subjects contain the test number from the loop
expect(emails.filter(it => it.subject.includes("Match subject test-1")).length).toEqual(1)
//</gen>
//<gen>wait_for_methods_test_wait_nth
const emailCount = await mailslurp.inboxController.getInboxEmailCount({inboxId: inbox.id})
const indexOfEmail0Based = emailCount.totalElements
await mailslurp.sendEmail(inbox.id, {
to: [inbox.emailAddress],
subject: "Next email"
})
const nthEmail = await mailslurp.waitController.waitForNthEmail({
inboxId: inbox.id,
index: indexOfEmail0Based
})
expect(nthEmail.subject).toContain('Next email');
//</gen>
}, 120_000);