Skip to content

Commit

Permalink
test(hashKey): add tests for hashKey()
Browse files Browse the repository at this point in the history
  • Loading branch information
gkalpak authored and ellimist committed Mar 15, 2017
1 parent 0a2a5cb commit abb26f3
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions test/ApiSpecs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,72 @@
'use strict';

describe('api', function() {
describe('hashKey()', function() {
it('should use an existing `$$hashKey`', function() {
var obj = {$$hashKey: 'foo'};
expect(hashKey(obj)).toBe('foo');
});

it('should support a function as `$$hashKey` (and call it)', function() {
var obj = {$$hashKey: valueFn('foo')};
expect(hashKey(obj)).toBe('foo');
});

it('should create a new `$$hashKey` if none exists (and return it)', function() {
var obj = {};
expect(hashKey(obj)).toBe(obj.$$hashKey);
expect(obj.$$hashKey).toBeDefined();
});

it('should create appropriate `$$hashKey`s for primitive values', function() {
expect(hashKey(undefined)).toBe(hashKey(undefined));
expect(hashKey(null)).toBe(hashKey(null));
expect(hashKey(null)).not.toBe(hashKey(undefined));
expect(hashKey(true)).toBe(hashKey(true));
expect(hashKey(false)).toBe(hashKey(false));
expect(hashKey(false)).not.toBe(hashKey(true));
expect(hashKey(42)).toBe(hashKey(42));
expect(hashKey(1337)).toBe(hashKey(1337));
expect(hashKey(1337)).not.toBe(hashKey(42));
expect(hashKey('foo')).toBe(hashKey('foo'));
expect(hashKey('foo')).not.toBe(hashKey('bar'));
});

it('should create appropriate `$$hashKey`s for non-primitive values', function() {
var fn = function() {};
var arr = [];
var obj = {};
var date = new Date();

expect(hashKey(fn)).toBe(hashKey(fn));
expect(hashKey(fn)).not.toBe(hashKey(function() {}));
expect(hashKey(arr)).toBe(hashKey(arr));
expect(hashKey(arr)).not.toBe(hashKey([]));
expect(hashKey(obj)).toBe(hashKey(obj));
expect(hashKey(obj)).not.toBe(hashKey({}));
expect(hashKey(date)).toBe(hashKey(date));
expect(hashKey(date)).not.toBe(hashKey(new Date()));
});

it('should support a custom `nextUidFn`', function() {
var nextUidFn = jasmine.createSpy('nextUidFn').and.returnValues('foo', 'bar', 'baz', 'qux');

var fn = function() {};
var arr = [];
var obj = {};
var date = new Date();

hashKey(fn, nextUidFn);
hashKey(arr, nextUidFn);
hashKey(obj, nextUidFn);
hashKey(date, nextUidFn);

expect(fn.$$hashKey).toBe('function:foo');
expect(arr.$$hashKey).toBe('object:bar');
expect(obj.$$hashKey).toBe('object:baz');
expect(date.$$hashKey).toBe('object:qux');
});
});

describe('HashMap', function() {
it('should do basic crud', function() {
Expand Down

0 comments on commit abb26f3

Please sign in to comment.