Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions frontend/__mocks__/localStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ let _localStorage = {};

(window as any).localStorage = (window as any).sessionStorage = {
setItem(key, value) {
return Object.assign(_localStorage, {[key]: value});
Object.assign(_localStorage, { [key]: value} );
},
getItem(key) {
return _localStorage[key];
return _localStorage[key] || null;
Copy link
Contributor Author

@vojtechszocs vojtechszocs Jul 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: these changes are made so that mock localStorage is as close to production as possible in terms of its API.

For example, in case of getItem method:

If the key does not exist, null is returned.

https://developer.mozilla.org/en-US/docs/Web/API/Storage/getItem

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should only return null if the key doesn't exist. If the value is an empty string, this will incorrectly return null.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should only return null if the key doesn't exist. If the value is an empty string, this will incorrectly return null.

Right, I've missed that. Will fix that in a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will fix that in a separate PR.

#1925

},
clear() {
_localStorage = {};
Expand Down
2 changes: 1 addition & 1 deletion frontend/__mocks__/matchMedia.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
window.matchMedia = () => ({matches: true});
window.matchMedia = () => ({ matches: true });
4 changes: 0 additions & 4 deletions frontend/__mocks__/requestAnimationFrame.js

This file was deleted.

7 changes: 4 additions & 3 deletions frontend/before-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import { configure } from 'enzyme';
import * as Adapter from 'enzyme-adapter-react-16';

import './setup-jsdom';
import 'url-search-params-polyfill';

// http://airbnb.io/enzyme/docs/installation/index.html#working-with-react-16
configure({adapter: new Adapter()});
configure({ adapter: new Adapter() });

window.SERVER_FLAGS = {
basePath: '/',
};

require('url-search-params-polyfill');
2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
"<rootDir>/node_modules/(?!lodash-es|@console)"
],
"testRegex": "/__tests__/.*\\.spec\\.(ts|tsx|js|jsx)$",
"testURL": "http://localhost",
"setupFiles": [
"./__mocks__/requestAnimationFrame.js",
"./__mocks__/localStorage.ts",
"./__mocks__/matchMedia.js",
"./before-tests.js"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ describe('BaseNode', () => {
.find('.odc-base-node__bg')
.first()
.props().filter,
).toBe('url(blank#BaseNodeDropShadowFilterId)');
).toBe('url(/#BaseNodeDropShadowFilterId)');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes are due to setting testURL value above.


wrapper.setState({ hover: true });
expect(
wrapper
.find('.odc-base-node__bg')
.first()
.props().filter,
).toBe('url(blank#BaseNodeDropShadowFilterId--hover)');
).toBe('url(/#BaseNodeDropShadowFilterId--hover)');
});

it('should show long labels when selected', () => {
Expand Down
3 changes: 3 additions & 0 deletions frontend/plugin-stats.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/* eslint-env node */

import './setup-jsdom';
import './__mocks__/matchMedia';

import {
resolvePluginPackages,
loadActivePlugins,
Expand Down
2 changes: 1 addition & 1 deletion frontend/public/reducers/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function getDefaultPerspective() {
activePerspective = defaultPerspective.properties.id;
}
}
return activePerspective;
return activePerspective || undefined;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to coerce the expectation (undefined means no perspective) vs. underlying storage implementation (null means no such item in storage).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is something breaking without this?
I'd rather not distinguish between null and undefined here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is something breaking without this?
I'd rather not distinguish between null and undefined here.

Without this, __tests__/reducers/ui.spec.ts fails on

expect(getDefaultPerspective()).toBeUndefined();

(expected undefined but received null)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bad test. null and undefined should be equivalent for this assertion.

}

export default (state: UIState, action: UIAction): UIState => {
Expand Down
27 changes: 27 additions & 0 deletions frontend/setup-jsdom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* eslint-env node */

// https://github.com/airbnb/enzyme/blob/master/docs/guides/jsdom.md

import { JSDOM } from 'jsdom';

const jsdom = new JSDOM('<!doctype html><html><body></body></html>');
const { window } = jsdom;

function copyProps(src, target) {
Object.defineProperties(target, {
...Object.getOwnPropertyDescriptors(src),
...Object.getOwnPropertyDescriptors(target),
});
}

global.window = window;
global.document = window.document;
global.navigator = {
userAgent: 'node.js',
};
global.requestAnimationFrame = (callback) => setTimeout(callback, 0);
global.cancelAnimationFrame = (id) => {
clearTimeout(id);
};

copyProps(window, global);