1
+ jest . mock ( 'net' )
2
+
3
+ describe ( 'Create new TcpServer' , ( ) => {
4
+
5
+ beforeEach ( ( ) => {
6
+ jest . resetModules ( ) ;
7
+ } )
8
+
9
+ it ( 'gived a correct connection should set connection and call addUser' , ( ) => {
10
+ const TcpServer = require ( '../TcpServer' )
11
+
12
+ const applicationAddUser = jest . fn ( )
13
+
14
+ const server = TcpServer . createNewTcpServer ( {
15
+ addUser : applicationAddUser
16
+ } )
17
+ expect ( server . on . mock . calls [ 0 ] [ 0 ] ) . toBe ( 'connection' )
18
+
19
+ const tcpServerConnectionCb = server . on . mock . calls [ 0 ] [ 1 ]
20
+ const connectionMock = {
21
+ setEncoding : jest . fn ( ) ,
22
+ on : jest . fn ( ) ,
23
+ remoteAddress : '127.0.0.1' ,
24
+ remotePort : 45664 ,
25
+ }
26
+
27
+ tcpServerConnectionCb ( connectionMock )
28
+
29
+ expect ( connectionMock . setEncoding . mock . calls [ 0 ] [ 0 ] ) . toBe ( 'utf-8' )
30
+ expect ( applicationAddUser . mock . calls . length ) . toBe ( 1 )
31
+ expect ( connectionMock . on . mock . calls [ 0 ] [ 0 ] ) . toBe ( 'data' )
32
+ expect ( connectionMock . on . mock . calls [ 1 ] [ 0 ] ) . toBe ( 'end' )
33
+ } ) ,
34
+
35
+ it ( 'gived an incorrect connection should not set connection and call addUser' , ( ) => {
36
+ const TcpServer = require ( '../TcpServer' )
37
+
38
+ const applicationAddUser = jest . fn ( )
39
+
40
+ const server = TcpServer . createNewTcpServer ( {
41
+ addUser : applicationAddUser
42
+ } )
43
+ expect ( server . on . mock . calls [ 0 ] [ 0 ] ) . toBe ( 'connection' )
44
+
45
+ const tcpServerConnectionCb = server . on . mock . calls [ 0 ] [ 1 ]
46
+ const connectionMock = {
47
+ setEncoding : jest . fn ( ) ,
48
+ on : jest . fn ( ) ,
49
+ remoteAddress : '127.0.0.1' ,
50
+ } // remotePort is missing
51
+
52
+ tcpServerConnectionCb ( connectionMock )
53
+
54
+ expect ( connectionMock . setEncoding . mock . calls . length ) . toBe ( 0 )
55
+ expect ( applicationAddUser . mock . calls . length ) . toBe ( 0 )
56
+ expect ( connectionMock . on . mock . calls . length ) . toBe ( 0 )
57
+ } )
58
+
59
+ it ( 'shoudl call the write func when an user send a message' , ( ) => {
60
+ const TcpServer = require ( '../TcpServer' )
61
+
62
+ const applicationAddUser = jest . fn ( )
63
+ const applicationBroadcastMessageByUser = jest . fn ( )
64
+
65
+ const server = TcpServer . createNewTcpServer ( {
66
+ addUser : applicationAddUser ,
67
+ broadcastMessageByUser : applicationBroadcastMessageByUser
68
+ } )
69
+ expect ( server . on . mock . calls [ 0 ] [ 0 ] ) . toBe ( 'connection' )
70
+
71
+ const tcpServerConnectionCb = server . on . mock . calls [ 0 ] [ 1 ]
72
+ const connectionMock = {
73
+ setEncoding : jest . fn ( ) ,
74
+ on : jest . fn ( ) ,
75
+ write : jest . fn ( ) ,
76
+ remoteAddress : '127.0.0.1' ,
77
+ remotePort : 3001
78
+ }
79
+
80
+ tcpServerConnectionCb ( connectionMock )
81
+ expect ( applicationAddUser . mock . calls . length ) . toBe ( 1 )
82
+
83
+ const connectionOnDataCb = connectionMock . on . mock . calls [ 0 ] [ 1 ]
84
+ connectionOnDataCb ( new Buffer ( 'Hello' ) )
85
+
86
+ expect ( applicationBroadcastMessageByUser . mock . calls . length ) . toBe ( 1 )
87
+
88
+ const userSendFunction = applicationBroadcastMessageByUser . mock . calls [ 0 ] [ 0 ] . send
89
+ userSendFunction ( 'Hello' )
90
+
91
+ expect ( connectionMock . write . mock . calls . length ) . toBe ( 1 )
92
+ } )
93
+
94
+ it ( 'shoudl call removeUser when a connection end' , ( ) => {
95
+ const TcpServer = require ( '../TcpServer' )
96
+
97
+ const applicationAddUser = jest . fn ( )
98
+ const applicationremoveUser = jest . fn ( )
99
+
100
+ const server = TcpServer . createNewTcpServer ( {
101
+ addUser : applicationAddUser ,
102
+ removeUser : applicationremoveUser
103
+ } )
104
+ expect ( server . on . mock . calls [ 0 ] [ 0 ] ) . toBe ( 'connection' )
105
+
106
+ const tcpServerConnectionCb = server . on . mock . calls [ 0 ] [ 1 ]
107
+ const connectionMock = {
108
+ setEncoding : jest . fn ( ) ,
109
+ on : jest . fn ( ) ,
110
+ remoteAddress : '127.0.0.1' ,
111
+ remotePort : 3001
112
+ }
113
+
114
+ tcpServerConnectionCb ( connectionMock )
115
+ expect ( applicationAddUser . mock . calls . length ) . toBe ( 1 )
116
+
117
+ const endConnectionCb = connectionMock . on . mock . calls [ 1 ] [ 1 ]
118
+ endConnectionCb ( { } )
119
+
120
+ expect ( applicationremoveUser . mock . calls . length ) . toBe ( 1 )
121
+ } )
122
+ } )
0 commit comments