Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit testing via the Jest framework
From now on we'll write unit tests to strengthen our code. I begin by writing tests for two simple files. In the process, I discover a bug in htmlquote (which I fix!) Code coverage is reported and lowest coverage possible is defined in package.json. We will work towards increasing this with time as we refactor. For now code coverage is 1%.. you have to start somewhere!! Fixes: #1999
- Loading branch information
Showing
7 changed files
with
106 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"env": { | ||
"es6": true, | ||
"jest": true | ||
}, | ||
"globals": { | ||
"test": true, | ||
"expect": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { foreach, range, join, len, htmlquote, enumerate, | ||
websafe } from '../../../openlibrary/plugins/openlibrary/js/jsdef'; | ||
|
||
test('jsdef: python range function', () => { | ||
expect(range(2, 5)).toEqual([2, 3, 4]); | ||
expect(range(5)).toEqual([0, 1, 2, 3, 4]); | ||
expect(range(0, 10, 2)).toEqual([0, 2, 4, 6, 8]); | ||
}); | ||
|
||
test('jsdef: enumerate', () => { | ||
expect(enumerate([1, 2, 3])).toEqual([ | ||
['0', 1], | ||
['1', 2], | ||
['2', 3] | ||
]); | ||
}); | ||
|
||
test('jsdef: foreach', () => { | ||
let called = 0; | ||
const loop = []; | ||
const listToLoop = [1, 2, 3]; | ||
expect.assertions(1); | ||
return new Promise((resolve) => { | ||
foreach(listToLoop, loop, function (val, i) { | ||
called += 1; | ||
if (called === 3) { | ||
expect(called).toBe(3); | ||
resolve(); | ||
} | ||
}) | ||
}); | ||
}); | ||
|
||
test('jsdef: join', () => { | ||
const str = '-'; | ||
const joinFn = join.bind(str); | ||
expect(joinFn(['1', '2'])).toBe('1-2'); | ||
}); | ||
|
||
test('jsdef: len', () => { | ||
expect(len(['1', '2'])).toBe(2); | ||
}); | ||
|
||
test('jsdef: htmlquote', () => { | ||
expect(htmlquote(5)).toBe('5'); | ||
expect(htmlquote('<foo>')).toBe('<foo>'); | ||
expect(htmlquote('\'foo\': "bar"')).toBe(''foo': "bar"'); | ||
expect(htmlquote('a&b')).toBe('a&b'); | ||
}); | ||
|
||
test('jsdef: websafe', () => { | ||
expect(websafe('<script>')).toBe('<script>'); | ||
// not sure if these are really necessary, but they document the current behaviour | ||
expect(websafe(undefined)).toBe(''); | ||
expect(websafe(null)).toBe(''); | ||
expect(websafe({toString: undefined})).toBe(''); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { commify } from '../../../openlibrary/plugins/openlibrary/js/python'; | ||
|
||
test( 'commify', () => { | ||
expect(commify('5443232')).toBe('5,443,232'); | ||
expect(commify('50')).toBe('50'); | ||
expect(commify('5000')).toBe('5,000'); | ||
expect(commify(['1','2','3','45'])).toBe('1,2,3,45'); | ||
expect(commify([1, 20, 3])).toBe('1,20,3'); | ||
} ); |