Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 028fa1a

Browse files
committed
test(hashKey): add tests for hashKey()
1 parent 3c259ce commit 028fa1a

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

test/ApiSpecs.js

+66
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,72 @@
11
'use strict';
22

33
describe('api', function() {
4+
describe('hashKey()', function() {
5+
it('should use an existing `$$hashKey`', function() {
6+
var obj = {$$hashKey: 'foo'};
7+
expect(hashKey(obj)).toBe('foo');
8+
});
9+
10+
it('should support a function as `$$hashKey` (and call it)', function() {
11+
var obj = {$$hashKey: valueFn('foo')};
12+
expect(hashKey(obj)).toBe('foo');
13+
});
14+
15+
it('should create a new `$$hashKey` if none exists (and return it)', function() {
16+
var obj = {};
17+
expect(hashKey(obj)).toBe(obj.$$hashKey);
18+
expect(obj.$$hashKey).toBeDefined();
19+
});
20+
21+
it('should create appropriate `$$hashKey`s for primitive values', function() {
22+
expect(hashKey(undefined)).toBe(hashKey(undefined));
23+
expect(hashKey(null)).toBe(hashKey(null));
24+
expect(hashKey(null)).not.toBe(hashKey(undefined));
25+
expect(hashKey(true)).toBe(hashKey(true));
26+
expect(hashKey(false)).toBe(hashKey(false));
27+
expect(hashKey(false)).not.toBe(hashKey(true));
28+
expect(hashKey(42)).toBe(hashKey(42));
29+
expect(hashKey(1337)).toBe(hashKey(1337));
30+
expect(hashKey(1337)).not.toBe(hashKey(42));
31+
expect(hashKey('foo')).toBe(hashKey('foo'));
32+
expect(hashKey('foo')).not.toBe(hashKey('bar'));
33+
});
34+
35+
it('should create appropriate `$$hashKey`s for non-primitive values', function() {
36+
var fn = function() {};
37+
var arr = [];
38+
var obj = {};
39+
var date = new Date();
40+
41+
expect(hashKey(fn)).toBe(hashKey(fn));
42+
expect(hashKey(fn)).not.toBe(hashKey(function() {}));
43+
expect(hashKey(arr)).toBe(hashKey(arr));
44+
expect(hashKey(arr)).not.toBe(hashKey([]));
45+
expect(hashKey(obj)).toBe(hashKey(obj));
46+
expect(hashKey(obj)).not.toBe(hashKey({}));
47+
expect(hashKey(date)).toBe(hashKey(date));
48+
expect(hashKey(date)).not.toBe(hashKey(new Date()));
49+
});
50+
51+
it('should support a custom `nextUidFn`', function() {
52+
var nextUidFn = jasmine.createSpy('nextUidFn').and.returnValues('foo', 'bar', 'baz', 'qux');
53+
54+
var fn = function() {};
55+
var arr = [];
56+
var obj = {};
57+
var date = new Date();
58+
59+
hashKey(fn, nextUidFn);
60+
hashKey(arr, nextUidFn);
61+
hashKey(obj, nextUidFn);
62+
hashKey(date, nextUidFn);
63+
64+
expect(fn.$$hashKey).toBe('function:foo');
65+
expect(arr.$$hashKey).toBe('object:bar');
66+
expect(obj.$$hashKey).toBe('object:baz');
67+
expect(date.$$hashKey).toBe('object:qux');
68+
});
69+
});
470

571
describe('HashMap', function() {
672
it('should do basic crud', function() {

0 commit comments

Comments
 (0)