|
<html> |
|
<head> |
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> |
|
<title>jasmine unit tests for flaten function</title> |
|
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/1.3.1/jasmine.min.css"> |
|
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/1.3.1/jasmine.min.js"></script> |
|
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/1.3.1/jasmine-html.min.js"></script> |
|
</head> |
|
<body> |
|
<script type="text/javascript"> |
|
window.onload=function(){ |
|
// source code |
|
const flatten = arr => arr.reduce( (acc, i) => `${acc + ' ' + i}` ).replace(/,/g, ' ').split(' ').map( i => parseInt(i, 10) ); |
|
|
|
// specs code |
|
describe('Unit tests v1', () => { |
|
it('should return 1 level array if array is 1 level depth ', () => { |
|
const array = [1,2,3,4,5,6]; |
|
expect(flatten(array)).toEqual(jasmine.objectContaining(array.flat())); |
|
}); |
|
|
|
it('should return array of one level for 2-level depth array', () => { |
|
const array = [ [1,2,3], [4,5,6] ]; |
|
expect(flatten(array)).toEqual(jasmine.objectContaining(array.flat())); |
|
}); |
|
|
|
it('should return array of one level for 3-level depth array', () => { |
|
const array = [ [1,[2,3]], [4,5,6] ]; |
|
expect(flatten(array)).toEqual(jasmine.objectContaining(array.flat(2))); |
|
}); |
|
|
|
it('should return array of one level for 4-level depth array', () => { |
|
const array = [ [1,[2,[3]]], [4,[5,[6]]] ]; |
|
expect(flatten(array)).toEqual(jasmine.objectContaining(array.flat(3))); |
|
}); |
|
|
|
it('should return array of one level for 9-level depth array', () => { |
|
const array = [ [1,[2,[[[[[[3]]]]]]]], [4,[5,[6]]] ]; |
|
expect(flatten(array)).toEqual(jasmine.objectContaining(array.flat(8))); |
|
}); |
|
}); |
|
|
|
// load jasmine htmlReporter |
|
(function() { |
|
var env = jasmine.getEnv(); |
|
env.addReporter(new jasmine.HtmlReporter()); |
|
env.execute(); |
|
}()); |
|
} |
|
</script> |
|
</body> |
|
</html> |