-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWebSocketConnection.test.ts
More file actions
166 lines (147 loc) · 6.64 KB
/
WebSocketConnection.test.ts
File metadata and controls
166 lines (147 loc) · 6.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { XComponent } from '../../src/XComponent';
import { WebSocket, Server } from 'mock-socket';
import { ErrorListener } from '../../src/interfaces/ErrorListener';
import Mock from '../utils/mockSubscriberDependencies';
import pako = require('pako');
import { generateUUID } from '../utils/uuid';
const encodeServerMessage = (strData: string) => {
let binaryString = pako.deflate(strData, { to: 'string' });
return window.btoa(binaryString);
};
describe('Test Connection module', function() {
let mockServer: Server | undefined;
beforeEach(function() {
// tslint:disable-next-line:no-any
(<any>window).WebSocket = WebSocket;
// tslint:disable-next-line:no-any
(<any>window).isTestEnvironnement = true;
});
afterEach((done) => {
if (mockServer) {
mockServer.close(); // Utiliser close() et non stop()
mockServer = undefined;
}
done(); // Pour Jest : signaler la fin du teardown
});
describe('Test createSession method', function() {
it('should call the sessionListener with the created session as argument', function(done: jest.DoneCallback) {
let serverUrl = 'wss://serverUrl';
mockServer = new Server(serverUrl);
let xcApiFileName = 'api.xcApi';
new XComponent()
.connect(serverUrl)
.then(connection => {
return connection.createSession(xcApiFileName);
})
.then(session => {
expect(session).not.toBe(null);
mockServer?.stop(done);
})
.catch(err => {
console.log(err);
});
// tslint:disable-next-line:no-any
mockServer.on('connection', function(server: any) {
server.on('message', function() {
const getApiResponse = `<deployment>
<clientAPICommunication>
</clientAPICommunication>
<codesConverter>
</codesConverter>
</deployment>`;
let content = encodeServerMessage(getApiResponse);
let data = { ApiFound: true, ApiName: xcApiFileName, Content: content };
server.send('getXcApi ' + JSON.stringify(data));
});
});
});
it('should provide meaningful error message when the Api is unknown', function(done: jest.DoneCallback) {
let serverUrl = 'wss://serverUrl1';
mockServer = new Server(serverUrl);
let xcApiFileName = 'unknownApi';
new XComponent().connect(serverUrl).then(connection => {
connection.createSession(xcApiFileName).catch(error => {
// it refers explicitly to the unknown Api on the error message, not to some random crash
expect(error.message).toMatch(xcApiFileName);
mockServer?.stop(done);
});
});
// tslint:disable-next-line:no-any
mockServer.on('connection', function(server: any) {
server.on('message', function() {
let data = { ApiFound: false, ApiName: xcApiFileName };
server.send('getXcApi ' + JSON.stringify(data));
});
});
});
it('when server stops after running in the first place, unexpectedCloseSessionErrorListener should be called', (done: jest.DoneCallback) => {
const serverUrl = 'wss://serverUrl';
mockServer = new Server(serverUrl);
const xcApiFileName = 'api.xcApi';
mockServer.on('connection', server => {
mockServer?.close(undefined);
});
new XComponent()
.connect(serverUrl, new FakeErrorHandler(err => done()))
.then(connection => {
connection.createSession(xcApiFileName);
})
.catch(error => {
/**/
});
});
// tslint:disable-next-line:typedef
it('given an unknown server url, should call the error listener', function(done) {
let serverUrl = 'wss://wrongServerUrl';
new XComponent().connect(serverUrl, new FakeErrorHandler(err => done())).catch(error => {
/**/
});
});
});
describe('Test getModel method', function() {
let serverMock: Server, serverUrl: string;
beforeEach(function() {
serverUrl = 'wss://' + generateUUID();
serverMock = new Server(serverUrl);
});
it('send getModel request, getModelListener callback should be executed when a response is received', function(done: jest.DoneCallback) {
new XComponent().connect(serverUrl).then(connection => {
let apiName = 'unknownApi';
connection.getCompositionModel(apiName).then(compositionModel => {
expect(compositionModel).not.toBe(undefined);
expect(compositionModel!.projectName).not.toBe(null);
expect(compositionModel!.components).not.toBe(null);
expect(compositionModel!.composition).not.toBe(null);
serverMock.stop(done);
});
});
// tslint:disable-next-line:no-any
serverMock.on('connection', function(server: any) {
server.on('message', function() {
server.send(Mock.getModelResponse);
});
});
});
it('send getModel request, getModelListener callback should be executed and receive undefined model and undefined graphical', function(done: jest.DoneCallback) {
new XComponent().connect(serverUrl).then(connection => {
let apiName = 'unknownApi';
connection.getCompositionModel(apiName).catch(() => {
serverMock.stop(done);
});
});
// tslint:disable-next-line:no-any
serverMock.on('connection', function(server: any) {
server.on('message', function() {
server.send(Mock.getModelResponseUndefined);
});
});
});
});
});
class FakeErrorHandler implements ErrorListener {
// tslint:disable-next-line:typedef
constructor(private done) {}
onError(err: Error) {
this.done();
}
}