Skip to content

Commit

Permalink
Merge pull request #1036 from WISE-Community/issue-1029-test-utilserv…
Browse files Browse the repository at this point in the history
…ice-makecopyofjson

Issue 1029 test UtilService.makeCopyOfJSON
  • Loading branch information
geoffreykwan authored Dec 20, 2017
2 parents 10c5f88 + 3fa95d4 commit 275b13b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 38 deletions.
51 changes: 32 additions & 19 deletions src/main/webapp/wise5/test-unit/services/utilService.spec.es6
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,67 @@ import angular from 'angular';
import mainModule from 'vle/main';
import 'angular-mocks';

describe('UtilService Unit Test', () => {
describe('UtilService', () => {

beforeEach(angular.mock.module(mainModule.name));

let UtilService;

beforeEach(inject((_UtilService_) => {
UtilService = _UtilService_;
}));

describe('UtilService', () => {
// Test UtilService.generateKey()
it('should return random keys', () => {
// Calling generate key with no params should return length 10 string
describe('generateKey()', () => {
it('should return random keys of length 10 by default', () => {
const generatedKey1 = UtilService.generateKey();
const generatedKey2 = UtilService.generateKey();
expect(generatedKey1.length).toEqual(10);
expect(generatedKey2.length).toEqual(10);
expect(generatedKey1).not.toEqual(generatedKey2);
});

// Calling generate key with length key should produce key with specified length
const genetatedKey3 = UtilService.generateKey(5);
expect(genetatedKey3.length).toEqual(5);
it('should return random keys of specified length', () => {
expect(UtilService.generateKey(5).length).toEqual(5);
expect(UtilService.generateKey(23).length).toEqual(23);
});

// Calling generate key 100 times should produce 100 unique random strings
it('should produce 100 unique random strings', () => {
const generatedKeysSoFar = [];
for (let i = 0; i < 100; i++) {
const generatedKey = UtilService.generateKey();
expect(generatedKeysSoFar.indexOf(generatedKey)).toEqual(-1);
generatedKeysSoFar.push(generatedKey);
}
});
});

// Test UtilService.convertStringToNumber()
it('should convert a string to a number', () => {
// Calling it with a number string should return the number
describe('convertStringToNumber()', () => {
it('should convert a number string to a number', () => {
expect(UtilService.convertStringToNumber("5")).toEqual(5);

// Calling it with a number string should return the number
expect(UtilService.convertStringToNumber("-100")).toEqual(-100);
});

// Calling it with null should return null
it('should return null for null argument', () => {
expect(UtilService.convertStringToNumber(null)).toBeNull();
});

// Calling it with a non-number string should return that non-number
it('should return non-null number string as is', () => {
expect(UtilService.convertStringToNumber("abc")).toEqual("abc");

// Calling it with a non-number string should return that non-number
expect(UtilService.convertStringToNumber("")).toEqual("");
});
})

describe('makeCopyOfJSONObject()', () => {
it('should copy an array object', () => {
const array1 = [1, 2, 3];
const copiedArray = UtilService.makeCopyOfJSONObject(array1);
expect(copiedArray).toEqual(array1);
});

it('should copy an object', () => {
const obj = {"name":"WISE", "address":"Berkeley"};
const copiedObj = UtilService.makeCopyOfJSONObject(obj);
expect(copiedObj).toEqual(obj);
});
});
});
49 changes: 31 additions & 18 deletions src/main/webapp/wise5/test-unit/services/utilService.spec.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 275b13b

Please sign in to comment.