1
1
const test = require ( 'node:test' ) ;
2
2
const assert = require ( 'assert' ) ;
3
3
const { Application, MailSystem } = require ( './main' ) ;
4
-
5
4
// TODO: write your tests here
6
- // Remember to use Stub, Mock, and Spy when necessary
5
+ // Remember to use Stub, Mock, and Spy when necessary
6
+ const test = require ( 'node:test' ) ;
7
+ const assert = require ( 'assert' ) ;
8
+ const { Application, MailSystem } = require ( './main' ) ;
9
+ //write tests use Stub, Mock, and Spy when necessary
10
+ const fs = require ( 'fs' ) ;
11
+ const util = require ( 'util' ) ;
12
+ const readFile = util . promisify ( fs . readFile ) ;
13
+ test . mock . method ( fs , 'readFile' , ( file , options , callback ) => {
14
+ callback ( null , 'john\njohn1\njohn2' ) ;
15
+ }
16
+ ) ;
17
+ //test mailSystem : write()
18
+ test ( 'should be able to write mail' , ( ) => {
19
+ const mailSystem = new MailSystem ( ) ;
20
+ const context = mailSystem . write ( 'test' ) ;
21
+ assert . strictEqual ( context , 'Congrats, test!' ) ;
22
+
23
+ } ) ;
24
+ // Test MailSystem : send()
25
+ test ( 'should be able to send mail' , ( ) => {
26
+ const mailSystem = new MailSystem ( ) ;
27
+ const success = mailSystem . send ( 'test' , 'test' ) ;
28
+ test . mock . method ( Math , 'random' , ( ) => 0.6 ) ;
29
+ assert . strictEqual ( mailSystem . send ( 'ok' , 'success' ) , true ) ;
30
+ test . mock . method ( Math , 'random' , ( ) => 0.4 ) ;
31
+ assert . strictEqual ( mailSystem . send ( 'fa' , 'fail' ) , false ) ;
32
+ } ) ;
33
+ // Test Application : getNames()
34
+ test ( 'should be able to get names' , async ( ) => {
35
+ const app = new Application ( ) ;
36
+ const names = await app . getNames ( ) ;
37
+ assert . ok ( names ) ;
38
+ } ) ;
39
+ // Test Application : getRandomPerson()
40
+ test ( 'should be able to get random person' , ( ) => {
41
+ const app = new Application ( ) ;
42
+ const person = app . getRandomPerson ( ) ;
43
+ test . mock . method ( Math , 'random' , ( ) => 0 ) ;
44
+ } ) ;
45
+ // Test Application : selectNextPerson()
46
+ test ( 'should be able to select next person' , ( ) => {
47
+ const app = new Application ( ) ;
48
+ const person = app . selectNextPerson ( ) ;
49
+ let cn = 0 ;
50
+ test . mock . method ( app , 'getRandomPerson' , ( ) => {
51
+ if ( cn <= person . length ) {
52
+ return person [ 0 ] [ cn ++ ] ;
53
+ }
54
+ } ) ;
55
+ } ) ;
56
+ // Test Application : notifySelected()
57
+ test ( 'should be able to notify selected' , ( ) => {
58
+ const app = new Application ( ) ;
59
+ app . notifySelected ( ) ;
60
+ assert . ok ( app ) ;
61
+ } ) ;
0 commit comments