1
- const test = require ( 'node:test' ) ;
1
+ const { describe , it , mock } = require ( 'node:test' ) ;
2
2
const assert = require ( 'assert' ) ;
3
- const { Application, MailSystem } = require ( './main' ) ;
3
+ const fs = require ( 'fs' ) ;
4
+ fs . readFile = ( fileName , options , callback ) => { process . nextTick ( ( ) => callback ( null , 'Alice\nBob\nCharlie' ) ) ; } ;
5
+ const { Application, MailSystem} = require ( './main' ) ;
4
6
5
7
// TODO: write your tests here
6
- // Remember to use Stub, Mock, and Spy when necessary
8
+ //test MailSystem
9
+ describe ( 'write test' , ( ) => {
10
+ it ( 'return expected string' , ( ) => {
11
+ const mailSystem = new MailSystem ( ) ;
12
+ assert . strictEqual ( mailSystem . write ( 'Bob' ) , 'Congrats, Bob!' ) ;
13
+ } ) ;
14
+ } ) ;
15
+ describe ( 'send test' , ( ) => {
16
+ it ( 'success send' , ( ) => {
17
+ const mailSystem = new MailSystem ( ) ;
18
+ const mockSuccess = mock . method ( Math , 'random' , ( ) => 1.0 ) ;
19
+ const result = mailSystem . send ( 'Bob' , 'Success' )
20
+ assert . strictEqual ( result , true ) ;
21
+ mockSuccess . mock . restore ( ) ;
22
+ } ) ;
23
+ it ( 'fail send' , ( ) => {
24
+ const mailSystem = new MailSystem ( ) ;
25
+ const mockFail = mock . method ( Math , 'random' , ( ) => 0 ) ;
26
+ const result = mailSystem . send ( 'Bob' , 'Fail' )
27
+ assert . strictEqual ( result , false ) ;
28
+ mockFail . mock . restore ( ) ;
29
+ } ) ;
30
+ } ) ;
31
+ //test Application
32
+ describe ( 'getNames test' , ( ) => {
33
+ it ( 'people & selected' , async ( ) => {
34
+ const app = new Application ( ) ;
35
+ await app . getNames ( ) ;
36
+ assert . deepStrictEqual ( app . people , [ 'Alice' , 'Bob' , 'Charlie' ] ) ;
37
+ assert . deepStrictEqual ( app . selected , [ ] ) ;
38
+ } ) ;
39
+ } ) ;
40
+ describe ( 'selectNextPerson test' , ( ) => {
41
+ it ( 'remain one person' , async ( ) => {
42
+ const app = new Application ( ) ;
43
+ await app . getNames ( ) ;
44
+ app . selected = [ 'Alice' , 'Bob' ] ;
45
+ assert . strictEqual ( app . selectNextPerson ( ) , 'Charlie' ) ;
46
+ } ) ;
47
+ it ( 'if all person selected' , async ( ) => {
48
+ const app = new Application ( ) ;
49
+ await app . getNames ( ) ;
50
+ app . selected = [ 'Alice' , 'Bob' , 'Charile' ] ;
51
+ assert . strictEqual ( app . selectNextPerson ( ) , null ) ;
52
+ } ) ;
53
+ } ) ;
54
+ describe ( 'notifySelected test' , ( ) => {
55
+ it ( 'no selected' , ( ) => {
56
+ const mailSystem = new MailSystem ;
57
+ const app = new Application ( ) ;
58
+ const mockWrite = mock . method ( mailSystem , 'write' , ( ) => "Mocked Message" ) ;
59
+ const mockSend = mock . method ( mailSystem , 'send' , ( ) => "Mocked Send" ) ;
60
+ app . mailSystem = mailSystem ;
61
+ app . notifySelected ( ) ;
62
+ assert . strictEqual ( mockWrite . mock . calls . length , 0 ) ;
63
+ assert . strictEqual ( mockSend . mock . calls . length , 0 ) ;
64
+ } ) ;
65
+ it ( 'selected all' , async ( ) => {
66
+ const mailSystem = new MailSystem ;
67
+ const app = new Application ( ) ;
68
+ const mockWrite = mock . method ( mailSystem , 'write' , ( ) => "Mocked Message" ) ;
69
+ const mockSend = mock . method ( mailSystem , 'send' , ( ) => "Mocked Send" ) ;
70
+ app . mailSystem = mailSystem ;
71
+ await app . getNames ( ) ;
72
+ app . selected = [ 'Alice' , 'Bob' , 'Charile' ] ;
73
+ app . notifySelected ( ) ;
74
+ assert . strictEqual ( mockWrite . mock . calls . length , 3 ) ;
75
+ assert . strictEqual ( mockSend . mock . calls . length , 3 ) ;
76
+ mockWrite . mock . restore ( ) ;
77
+ mockSend . mock . restore ( ) ;
78
+ } ) ;
79
+ } ) ;
80
+ // Remember to use Stub, Mock, and Spy when necessary
81
+
0 commit comments