1
1
const test = require ( 'node:test' ) ;
2
2
const assert = require ( 'assert' ) ;
3
3
const { Application, MailSystem } = require ( './main' ) ;
4
+ const sinon = require ( 'sinon' ) ;
4
5
5
- // TODO: write your tests here
6
- // Remember to use Stub, Mock, and Spy when necessary
6
+ test ( 'test getRandomPerson in main.js' , async ( ) => {
7
+ const app = new Application ( ) ;
8
+
9
+ app . getNames = async ( ) => {
10
+ app . people = [ 'Annika' , 'Billy' , 'Cecilia' ] ;
11
+ } ;
12
+
13
+ await app . getNames ( ) ;
14
+
15
+ const person = app . getRandomPerson ( ) ;
16
+ assert . ok ( app . people . includes ( person ) , 'the person is not in list' ) ;
17
+ } ) ;
18
+
19
+ test ( 'test mains selectNextPerson to not select same person twice' , async ( ) => {
20
+ const app = new Application ( ) ;
21
+
22
+ app . getNames = async ( ) => {
23
+ app . people = [ 'Annika' , 'Billy' , 'Cecilia' ] ;
24
+ app . selected = [ ] ;
25
+ } ;
26
+
27
+ await app . getNames ( ) ;
28
+
29
+ const person1 = app . selectNextPerson ( ) ;
30
+ const person2 = app . selectNextPerson ( ) ;
31
+
32
+ assert . notStrictEqual ( person1 , person2 , 'Same person was selected twice' ) ;
33
+ } ) ;
34
+
35
+ test ( 'test mains notifySelected that it calls send()' , async ( ) => {
36
+ const mailSystem = new MailSystem ( ) ;
37
+ const sendSpy = sinon . spy ( mailSystem , 'send' ) ;
38
+
39
+ const app = new Application ( ) ;
40
+ app . mailSystem = mailSystem ;
41
+
42
+ app . getNames = async ( ) => {
43
+ app . people = [ 'Annika' , 'Billy' , 'Cecilia' ] ;
44
+ app . selected = [ ] ;
45
+ } ;
46
+
47
+ await app . getNames ( ) ;
48
+
49
+ const person = app . selectNextPerson ( ) ;
50
+ app . selected = [ person ] ;
51
+
52
+ await app . notifySelected ( ) ;
53
+
54
+ assert . ok (
55
+ sendSpy . calledWith ( person , `Congrats, ${ person } !` ) ,
56
+ `send() didn't work with args. Captured calls: ${ JSON . stringify ( sendSpy . args ) } `
57
+ ) ;
58
+ } ) ;
0 commit comments