-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExampleFeature.test.ts
179 lines (140 loc) · 6.52 KB
/
ExampleFeature.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
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
177
178
179
import "reflect-metadata";
import Comment from "snoowrap/dist/objects/Comment";
import PrivateMessage from "snoowrap/dist/objects/PrivateMessage";
import RedditUser from "snoowrap/dist/objects/RedditUser";
import Submission from "snoowrap/dist/objects/Submission";
import { mocked } from "ts-jest/utils";
import ExampleFeature from "../../../src/features/exampleFeature/ExampleFeature";
import Command from "../../../src/shared/commands/Command";
import Logger from "../../../src/shared/logging/Logger";
jest.mock("../../../src/shared/logging/Logger");
jest.mock("snoowrap/dist/objects/Comment");
jest.mock("snoowrap/dist/objects/RedditUser");
jest.mock("snoowrap/dist/objects/Submission");
jest.mock("snoowrap/dist/objects/PrivateMessage");
describe('onComment', () => {
const dummyAuthorName = 'dummyAuthorName';
const dummyBody = 'dummyBody';
const dummyBodyWithHello = 'dummyBody hello';
test('logs comment details', async () => {
// Arrange
const mockRedditUser = new RedditUser(null, null, null);
mockRedditUser.name = dummyAuthorName;
const mockComment = new Comment(null, null, null);
mockComment.author = mockRedditUser;
mockComment.body = dummyBody;
const mockLogger = new Logger(null);
const testSubject = new ExampleFeature(mockLogger);
// Act
await testSubject.onComment(mockComment, null);
// Assert
const mockLoggerTyped = mocked(mockLogger);
expect(mockLoggerTyped.info.mock.calls[0][0]).toEqual(`onComment, author: ${dummyAuthorName}, comment body: ${dummyBody}`);
});
test('replies comment if it contains "hello"', async () => {
// Arrange
const mockComment = new Comment(null, null, null);
mockComment.author = new RedditUser(null, null, null);
mockComment.body = dummyBodyWithHello;
const testSubject = new ExampleFeature(new Logger(null));
// Act
await testSubject.onComment(mockComment, null);
// Assert
const mockCommentTyped = mocked(mockComment);
expect(mockCommentTyped.reply.mock.calls[0][0]).toEqual('Hi!');
});
test('ignores comment if it does not contain "hello"', async () => {
// Arrange
const mockComment = new Comment(null, null, null);
mockComment.author = new RedditUser(null, null, null);
mockComment.body = dummyBody;
const testSubject = new ExampleFeature(new Logger(null));
// Act
await testSubject.onComment(mockComment, null);
// Assert
const mockCommentTyped = mocked(mockComment);
expect(mockCommentTyped.reply.mock.calls.length).toEqual(0);
});
});
describe('onSubmission', () => {
const dummyAuthorName = 'dummyAuthorName';
const dummyTitle = 'dummyTitle';
const dummyTitleWithHello = 'dummyTitle hello';
test('logs submission details', async () => {
// Arrange
const mockRedditUser = new RedditUser(null, null, null);
mockRedditUser.name = dummyAuthorName;
const mockSubmission = new Submission(null, null, null);
mockSubmission.author = mockRedditUser;
mockSubmission.title = dummyTitle;
const mockLogger = new Logger(null);
const testSubject = new ExampleFeature(mockLogger);
// Act
await testSubject.onSubmission(mockSubmission);
// Assert
const mockLoggerTyped = mocked(mockLogger);
expect(mockLoggerTyped.info.mock.calls[0][0]).toEqual(`onSubmission, author: ${dummyAuthorName}, submission title: ${dummyTitle}`);
});
test('replies submission if its title contains "hello"', async () => {
// Arrange
const mockSubmission = new Submission(null, null, null);
mockSubmission.author = new RedditUser(null, null, null);
mockSubmission.title = dummyTitleWithHello;
const testSubject = new ExampleFeature(new Logger(null));
// Act
await testSubject.onSubmission(mockSubmission);
// Assert
const mockSubmissionTyped = mocked(mockSubmission);
expect(mockSubmissionTyped.reply.mock.calls[0][0]).toEqual('Hi!');
});
test('ignores submission if its titles does not contain "hello"', async () => {
// Arrange
const mockSubmission = new Submission(null, null, null);
mockSubmission.author = new RedditUser(null, null, null);
mockSubmission.title = dummyTitle;
const testSubject = new ExampleFeature(new Logger(null));
// Act
await testSubject.onSubmission(mockSubmission);
// Assert
const mockSubmissionTyped = mocked(mockSubmission);
expect(mockSubmissionTyped.reply.mock.calls.length).toEqual(0);
});
});
describe('onPrivateMessage', () => {
test('logs private message details and replies', async () => {
// Arrange
const dummyAuthorName = 'dummyAuthorName';
const dummyBody = 'dummyBody';
const dummyCommandName = 'dummyCommandName';
const dummyCommandOptionName = 'dummyCommandOptionName';
const dummyCommandOptionValue = 'dummyCommandOptionValue';
const dummyCommand = new Command(dummyCommandName, new Map<string, string>([[dummyCommandOptionName, dummyCommandOptionValue]]));
const mockRedditUser = new RedditUser(null, null, null);
mockRedditUser.name = dummyAuthorName;
const mockPrivateMessage = new PrivateMessage(null, null, null);
mockPrivateMessage.author = mockRedditUser;
mockPrivateMessage.body = dummyBody;
const mockLogger = new Logger(null);
const testSubject = new ExampleFeature(mockLogger);
// Act
await testSubject.onPrivateMessage(mockPrivateMessage, dummyCommand);
// Assert
const mockLoggerTyped = mocked(mockLogger);
expect(mockLoggerTyped.info.mock.calls[0][0]).toEqual(`onPrivateMessage, author: ${dummyAuthorName}, private message body: ${dummyBody},\
command: ${dummyCommandName}, command options: ${dummyCommandOptionName}=${dummyCommandOptionValue}`);
const mockPrivateMessageTyped = mocked(mockPrivateMessage);
expect(mockPrivateMessageTyped.reply.mock.calls[0][0]).toEqual(`echo: ${dummyBody}`);
});
});
describe('onInit', () => {
test('logs', async () => {
// Arrange
const mockLogger = new Logger(null);
const testSubject = new ExampleFeature(mockLogger);
// Act
await testSubject.onInit();
// Assert
const mockLoggerTyped = mocked(mockLogger);
expect(mockLoggerTyped.info.mock.calls[0][0]).toEqual('onInit');
});
});