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