Skip to content
This repository has been archived by the owner on Aug 15, 2019. It is now read-only.

Unit tests #67

Merged
merged 22 commits into from
Aug 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
d98ca3a
test(getAttribute.test.js): adding unit tests for getAttribute
biharck Aug 3, 2019
4ff10fc
feat(getAuthorizationHeader.test.js): including getAuthorizationHeade…
biharck Aug 4, 2019
78b2950
feat(getName.test.js): adding getName tests
biharck Aug 4, 2019
8bb8d01
feat(getNumber.test.js): adding getNumber.test.js
biharck Aug 4, 2019
2bb3be5
feat(getString.test.js): adding getString.test.js
biharck Aug 4, 2019
4bdfc5a
feat(index.test.js): adding index.test.js file
biharck Aug 4, 2019
9ec7f11
feat(index.test.js): improviments on index test file
biharck Aug 4, 2019
94e9323
feat(getModalities.test.js): adding getModalities tests
biharck Aug 4, 2019
7e35154
feat(index.test.js): emoving call tests since the expect test already…
biharck Aug 6, 2019
58957ea
adding and refactoring absoluteUrl function
biharck Aug 7, 2019
e5b1056
adding addServer.test.js
biharck Aug 8, 2019
c497001
adding unit tests for guid
biharck Aug 8, 2019
438e592
adding unit tests for index.js
biharck Aug 8, 2019
e3248a8
fixing typo in guid.test.js and adding isImage.test.js file
biharck Aug 8, 2019
e43ae2f
adding tests for objectPath.js
biharck Aug 8, 2019
bfd062f
replacing toBeCalled to toBeCalledWith
biharck Aug 9, 2019
8918c50
Merge branch 'master' into unit-tests
biharck Aug 9, 2019
b157816
fixing typo
biharck Aug 9, 2019
91658db
removing beforeEach
biharck Aug 9, 2019
e76725c
making the test description and variables clear
biharck Aug 9, 2019
8ec4b54
Clearing all mocks after tests run
biharck Aug 9, 2019
c959b12
improving addServer tests
biharck Aug 9, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/utils/absoluteUrl.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export default function absoluteUrl(path) {
const absoluteUrl = path => {
let absolutePath = '/';

if (!path) return absolutePath;

// TODO: Find another way to get root url
const absoluteUrl = window.location.origin;
const absoluteUrlParts = absoluteUrl.split('/');
Expand All @@ -13,4 +15,6 @@ export default function absoluteUrl(path) {
}

return absolutePath.replace(/\/\/+/g, '/');
}
};

export default absoluteUrl;
51 changes: 51 additions & 0 deletions src/utils/absoluteUrl.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import absoluteUrl from './absoluteUrl';

describe('absoluteUrl', () => {
test('should return /path_1/path_2/path_3/path_to_destination when the window.location.origin is http://dummy.com/path_1/path_2 and the path is /path_3/path_to_destination', () => {
let global = {
window: Object.create(window),
};
const url = 'http://dummy.com/path_1/path_2';
Object.defineProperty(window, 'location', {
value: {
origin: url,
},
writable: true,
});
const absoluteUrlOutput = absoluteUrl('/path_3/path_to_destination');
expect(absoluteUrlOutput).toEqual(
'/path_1/path_2/path_3/path_to_destination'
dannyrb marked this conversation as resolved.
Show resolved Hide resolved
);
});

test('should return / when the path is not defined', () => {
const absoluteUrlOutput = absoluteUrl(undefined);
expect(absoluteUrlOutput).toBe('/');
});

test('should return the original path when there path in the window.origin after the domain and port', () => {
global.window = Object.create(window);
const url = 'http://dummy.com';
Object.defineProperty(window, 'location', {
value: {
origin: url,
},
writable: true,
});
const absoluteUrlOutput = absoluteUrl('path_1/path_2/path_3');
expect(absoluteUrlOutput).toEqual('/path_1/path_2/path_3');
});

test('should be able to return the absolute path even when the path contains duplicates', () => {
global.window = Object.create(window);
const url = 'http://dummy.com';
Object.defineProperty(window, 'location', {
value: {
origin: url,
},
writable: true,
});
const absoluteUrlOutput = absoluteUrl('path_1/path_1/path_1');
expect(absoluteUrlOutput).toEqual('/path_1/path_1/path_1');
});
});
82 changes: 82 additions & 0 deletions src/utils/addServer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import addServers from './addServers';

describe('addServers', () => {
const servers = {
dicomWeb: [
{
name: 'DCM4CHEE',
wadoUriRoot: 'https://server.dcmjs.org/dcm4chee-arc/aets/DCM4CHEE/wado',
qidoRoot: 'https://server.dcmjs.org/dcm4chee-arc/aets/DCM4CHEE/rs',
wadoRoot: 'https://server.dcmjs.org/dcm4chee-arc/aets/DCM4CHEE/rs',
qidoSupportsIncludeField: true,
imageRendering: 'wadors',
thumbnailRendering: 'wadors',
requestOptions: {
requestFromBrowser: true,
},
},
],
oidc: [
{
authority: 'http://127.0.0.1/auth/realms/ohif',
client_id: 'ohif-viewer',
redirect_uri: 'http://127.0.0.1/callback',
response_type: 'code',
scope: 'openid',
post_logout_redirect_uri: '/logout-redirect.html',
},
],
};

const store = {
dispatch: jest.fn(),
};

test('should be able to add a server and dispatch to the store successfuly', () => {
addServers(servers, store);
expect(store.dispatch).toBeCalledWith({
server: {
authority: 'http://127.0.0.1/auth/realms/ohif',
client_id: 'ohif-viewer',
post_logout_redirect_uri: '/logout-redirect.html',
redirect_uri: 'http://127.0.0.1/callback',
response_type: 'code',
scope: 'openid',
type: 'oidc',
},
type: 'ADD_SERVER',
});
expect(store.dispatch).toBeCalledWith({
server: {
imageRendering: 'wadors',
name: 'DCM4CHEE',
qidoRoot: 'https://server.dcmjs.org/dcm4chee-arc/aets/DCM4CHEE/rs',
qidoSupportsIncludeField: true,
requestOptions: { requestFromBrowser: true },
thumbnailRendering: 'wadors',
type: 'dicomWeb',
wadoRoot: 'https://server.dcmjs.org/dcm4chee-arc/aets/DCM4CHEE/rs',
wadoUriRoot: 'https://server.dcmjs.org/dcm4chee-arc/aets/DCM4CHEE/wado',
},
type: 'ADD_SERVER',
});
});

test('should throw an error if servers list is not defined', () => {
expect(() => addServers(undefined, store)).toThrowError(
new Error('The servers and store must be defined')
);
});

test('should throw an error if store is not defined', () => {
expect(() => addServers(servers, undefined)).toThrowError(
new Error('The servers and store must be defined')
);
});

test('should throw an error when both server and store are not defined', () => {
expect(() => addServers(undefined, undefined)).toThrowError(
new Error('The servers and store must be defined')
);
});
});
10 changes: 8 additions & 2 deletions src/utils/addServers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// TODO: figure out where else to put this function
export default function addServers(servers, store) {
const addServers = (servers, store) => {
if (!servers || !store) {
throw new Error('The servers and store must be defined');
}

Object.keys(servers).forEach(serverType => {
const endpoints = servers[serverType];
endpoints.forEach(endpoint => {
Expand All @@ -12,4 +16,6 @@ export default function addServers(servers, store) {
});
});
});
}
};

export default addServers;
26 changes: 14 additions & 12 deletions src/utils/guid.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@
*
* @return {string}
*/
export default function guid() {
function s4() {
const guid = () => {
const getFourRandomValues = () => {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
};
return (
s4() +
s4() +
getFourRandomValues() +
getFourRandomValues() +
'-' +
s4() +
getFourRandomValues() +
'-' +
s4() +
getFourRandomValues() +
'-' +
s4() +
getFourRandomValues() +
'-' +
s4() +
s4() +
s4()
getFourRandomValues() +
getFourRandomValues() +
getFourRandomValues()
);
}
};

export default guid;
46 changes: 46 additions & 0 deletions src/utils/guid.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import guid from './guid';

describe('guid', () => {
Math.random = jest.fn(() => 0.4677647565236618);
Copy link
Member

Choose a reason for hiding this comment

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

Will this bleed into a different test suite? Do you need to "undo" this patch after all of these tests have run?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. It won't right now but it is safe to add a clearAllMocks

Copy link
Member

Choose a reason for hiding this comment

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

@biharck, if you use Math.random somewhere else, I think it would still be a jest.fn? Doesn't clearAllMocks just reset the function so you can see what args are passed?

I think you need to do something like...

const savedFn = Math.random;
Math.random = jest.fn();
// after all
Math.random = savedFn;

Copy link
Member

Choose a reason for hiding this comment

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

For example: jestjs/jest#7136 (comment)

But I think you're right that Math.random calls in other test suites would use the baked in implementation, and not the one we override here.

const guidValue = guid();

afterAll(() => {
jest.clearAllMocks();
});

test('should return 77bf77bf-77bf-77bf-77bf-77bf77bf77bf when the random value is fixed on 0.4677647565236618', () => {
expect(guidValue).toBe('77bf77bf-77bf-77bf-77bf-77bf77bf77bf');
});

test('should always return a guid of size 36', () => {
expect(guidValue.length).toBe(36);
});

test('should always return a guid with five sequences', () => {
expect(guidValue.split('-').length).toBe(5);
});

test('should always return a guid with four dashes', () => {
expect(guidValue.split('-').length - 1).toBe(4);
});

test('should return the first sequence with length of eigth', () => {
expect(guidValue.split('-')[0].length).toBe(8);
});

test('should return the second sequence with length of four', () => {
expect(guidValue.split('-')[1].length).toBe(4);
});

test('should return the third sequence with length of four', () => {
expect(guidValue.split('-')[2].length).toBe(4);
});

test('should return the fourth sequence with length of four', () => {
expect(guidValue.split('-')[3].length).toBe(4);
});

test('should return the last sequence with length of twelve', () => {
expect(guidValue.split('-')[4].length).toBe(12);
});
});
23 changes: 23 additions & 0 deletions src/utils/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as utils from './index.js';

describe('Top level exports', () => {
test('should export the modules ', () => {
const expectedExports = [
'guid',
'ObjectPath',
'absoluteUrl',
'addServers',
'sortBy',
'writeScript',
'StackManager',
'studyMetadataManager',
// Updates WADO-RS metaDataManager
'updateMetaDataManager',
'DICOMTagDescriptions',
].sort();

const exports = Object.keys(utils.default).sort();

expect(exports).toEqual(expectedExports);
});
});