This repository was archived by the owner on Dec 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsubscription.test.js
More file actions
108 lines (91 loc) · 3.06 KB
/
subscription.test.js
File metadata and controls
108 lines (91 loc) · 3.06 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
/* eslint-disable */
process.env.MAILGUN_API_KEY = "api_key"
process.env.MAILGUN_DOMAIN = "domain"
describe('usage', function() {
jest.resetModules()
jest.mock('./logger')
const logger = require('logger')
jest.mock('mailgun-js')
const mailgun = require('mailgun-js')
const mockMailgun = {
lists: jest.fn(() => mockMailgun),
members: jest.fn(() => mockMailgun),
update: jest.fn(() => mockMailgun),
}
mailgun.mockReturnValue(mockMailgun)
const m = require('./subscription')(mockMailgun)
test('subscribe changes the correct list', () => {
m.subscribe('email')
expect(mockMailgun.lists).toBeCalledWith(`users@${process.env.MAILGUN_DOMAIN}`)
})
test('unsubscribe changes the correct list', () => {
m.unsubscribe('email')
expect(mockMailgun.lists).toBeCalledWith(`users@${process.env.MAILGUN_DOMAIN}`)
})
test('unsubscribe sets subscribed=false', () => {
mockMailgun.update.mockClear()
m.unsubscribe('email')
expect(mockMailgun.update).toBeCalled()
expect(mockMailgun.update.mock.calls[0][0].subscribed).toBe(false)
})
test('subscribe sets subscribed=true', () => {
mockMailgun.update.mockClear()
m.subscribe('email')
expect(mockMailgun.update).toBeCalled()
expect(mockMailgun.update.mock.calls[0][0].subscribed).toBe(true)
})
})
describe('error handling', function() {
jest.resetModules()
jest.mock('mailgun-js')
const mailgun = require('mailgun-js')
const mockMailgun = {
lists: jest.fn(() => mockMailgun),
members: jest.fn(() => mockMailgun),
update: jest.fn((opts, cb) => cb('error', null)),
}
mailgun.mockReturnValue(mockMailgun)
jest.mock('./logger')
const logger = require('./logger')
logger.error = jest.fn()
const m = require('./subscription')(mockMailgun)
test('subscribe', () => {
logger.error.mockClear()
m.subscribe('email')
expect(logger.error).toBeCalled()
expect(logger.error.mock.calls[0][0]).toBe(`could not subscribe : email : error : null`)
})
test('unsubscribe', () => {
logger.error.mockClear()
m.unsubscribe('email')
expect(logger.error).toBeCalled()
expect(logger.error.mock.calls[0][0]).toBe(`could not unsubscribe : email : error : null`)
})
})
describe('success handling', () => {
jest.resetModules()
jest.mock('mailgun-js')
const mailgun = require('mailgun-js')
const mockMailgun = {
lists: jest.fn(() => mockMailgun),
members: jest.fn(() => mockMailgun),
update: jest.fn((opts, cb) => cb(null, 'success')),
}
mailgun.mockReturnValue(mockMailgun)
jest.mock('./logger')
const logger = require('./logger')
logger.debug = jest.fn()
const m = require('./subscription')(mockMailgun)
test('unsubscribe', () => {
logger.debug.mockClear()
m.unsubscribe('email')
expect(logger.debug).toBeCalled()
expect(logger.debug.mock.calls[0][0]).toBe(`unsubscribed : email : success`)
})
test('subscribe', () => {
logger.debug.mockClear()
m.subscribe('email')
expect(logger.debug).toBeCalled()
expect(logger.debug.mock.calls[0][0]).toBe(`subscribed : email : success`)
})
})