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

Commit

Permalink
test: Misc. utilities
Browse files Browse the repository at this point in the history
* test(getAttribute.test.js): adding unit tests for getAttribute

* feat(getAuthorizationHeader.test.js): including getAuthorizationHeader.test.js

* feat(getName.test.js): adding getName tests

* feat(getNumber.test.js): adding getNumber.test.js

* feat(getString.test.js): adding getString.test.js

* feat(index.test.js): adding index.test.js file

* feat(index.test.js): improviments on index test file

* feat(getModalities.test.js): adding getModalities tests

* feat(index.test.js): emoving call tests since the expect test already covers it

* adding and refactoring absoluteUrl function

* adding addServer.test.js

* adding unit tests for guid

* adding unit tests for index.js

* fixing typo in guid.test.js and adding isImage.test.js file

* adding tests for objectPath.js

* replacing toBeCalled to toBeCalledWith

* fixing typo

* removing beforeEach

* making the test description and variables clear

* Clearing all mocks after tests run

* improving addServer tests
  • Loading branch information
biharck authored and dannyrb committed Aug 10, 2019
1 parent 60ca6f0 commit d126655
Show file tree
Hide file tree
Showing 10 changed files with 717 additions and 85 deletions.
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'
);
});

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);
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);
});
});

0 comments on commit d126655

Please sign in to comment.