Skip to content

Commit 4972011

Browse files
committed
Fix removing cookie after close success toaster.
CW-1927
1 parent a20326c commit 4972011

5 files changed

Lines changed: 39 additions & 3 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@
152152
"redux-observable": "^0.14.0",
153153
"redux-thunk": "^2.1.0",
154154
"reselect": "^2.5.4",
155-
"rxjs": "^5.2.0"
155+
"rxjs": "^5.2.0",
156+
"mock-socket": "latest"
156157
}
157158
}

src/SuccessToaster/components/WebSocketTerminal.jsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* globals document */
12
import React, {Component} from 'react';
23

34
import Console from '../../components/Console';
@@ -45,13 +46,15 @@ export class WebSocketTerminal extends Component {
4546
componentDidMount() {
4647
const {url, authToken} = this.props;
4748

48-
document.cookie = `Auth-Token=${authToken}; path=/`; // eslint-disable-line no-undef
49+
document.cookie = `Auth-Token=${authToken}; path=/`;
4950

5051
this.setConnection(url);
5152
}
5253

5354
componentWillUnmount() {
5455
this.closeConnection();
56+
57+
document.cookie = 'Auth-Token= ; path=/; expires = Thu, 01 Jan 1970 00:00:00 GMT';
5558
}
5659

5760
setConnection = url => {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* global it, describe, document */
2+
import React from 'react';
3+
import chai from 'chai';
4+
import chaiEnzyme from 'chai-enzyme';
5+
import {mount} from 'enzyme';
6+
7+
import WebSocketTerminal from './WebSocketTerminal';
8+
9+
chai.use(chaiEnzyme());
10+
chai.should();
11+
12+
describe('WebSocketTerminal', () => {
13+
describe('componentDidMount', () => {
14+
it('should set cookie', () => {
15+
mount(<WebSocketTerminal url={'https://localhost:6123'} authToken={'SampleAuthToken'}/>);
16+
17+
document.cookie.should.equal('Auth-Token=SampleAuthToken');
18+
});
19+
});
20+
21+
describe('componentWillUnmount', () => {
22+
it('should remove cookie', () => {
23+
const wrapper = mount(<WebSocketTerminal url={'https://localhost:6123'} authToken={'SampleAuthToken'}/>);
24+
wrapper.unmount();
25+
26+
document.cookie.should.equal('');
27+
});
28+
});
29+
});

src/api/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ export const createWebSocket = (url, events = {
244244
url = url.replace('http', 'ws'); // todo: add ^
245245
}
246246

247-
const ws = new WebSocket(url); // eslint-disable-line
247+
const ws = new window.WebSocket(url); // eslint-disable-line
248248

249249
ws.addEventListener('open', events.onOpen);
250250
ws.addEventListener('message', events.onMessage);

test/helpers/dom.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require('mock-local-storage');
22
var jsdom = require('jsdom');
3+
var mockSocket = require('mock-socket');
34

45
// setup the simplest document possible
56
var doc = jsdom.jsdom('<!doctype html><html><body></body></html>', {url: 'http://localhost/'});
@@ -8,6 +9,7 @@ var win = doc.defaultView;
89
// set globals for mocha that make access to document and window feel
910
// natural in the test environment
1011
global.document = doc;
12+
global.WebSocket = mockSocket.WebSocket;
1113
global.window = win;
1214

1315
// take all properties of the window object and also attach it to the
@@ -30,4 +32,5 @@ function propagateToGlobal(window) {
3032
}
3133
window.sessionStorage = global.sessionStorage;
3234
window.localStorage = global.localStorage;
35+
window.WebSocket = global.WebSocket;
3336
}

0 commit comments

Comments
 (0)