Skip to content

Commit be2ae05

Browse files
committed
Add tests for hash calculator
1 parent d5567f5 commit be2ae05

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import * as assert from "assert";
2+
3+
import { HashCalculator } from "../../../src/Documents/Queries/HashCalculator";
4+
import { TypesAwareObjectMapper } from "../../../src/Mapping/ObjectMapper";
5+
6+
const mockObjectMapper = {
7+
toObjectLiteral: obj => obj.toString()
8+
} as TypesAwareObjectMapper;
9+
10+
const hash = data => {
11+
const calculator = new HashCalculator();
12+
calculator.write(data, mockObjectMapper);
13+
return calculator.getHash();
14+
}
15+
16+
describe('Hash calculator tests', () => {
17+
it('Calculates the same hash for the same object', async () => {
18+
const obj = {
19+
boolean: true,
20+
function: () => {
21+
console.log('No-op')
22+
},
23+
number: 4,
24+
object: {
25+
property: 'value'
26+
},
27+
string: 'hello',
28+
symbol: Symbol('world'),
29+
undefined: undefined,
30+
};
31+
32+
assert.equal(hash(obj), hash(obj));
33+
assert.equal(hash(obj), hash({ ...obj }));
34+
});
35+
36+
it('Calculates different hashes for different types', async () => {
37+
assert.notEqual(hash(1), hash(true))
38+
assert.notEqual(hash('1'), hash(true))
39+
assert.notEqual(hash(1), hash('1'))
40+
});
41+
42+
it('Calculates different hashes for different numbers', async () => {
43+
assert.notEqual(hash(1), hash(257));
44+
assert.notEqual(hash(86400), hash(0));
45+
});
46+
});

0 commit comments

Comments
 (0)