summaryrefslogtreecommitdiffstats
path: root/js/vendor/angular-ui/modules/filters/unique/test/uniqueSpec.js
blob: ae1251f4b851b61e521a0ba924ea9cb3b29ae320 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
describe('unique', function () {
  var uniqueFilter;

  beforeEach(module('ui.filters'));
  beforeEach(inject(function ($filter) {
    uniqueFilter = $filter('unique');
  }));

  it('should return unique entries based on object equality', function () {
    var arrayToFilter = [
      {key: 'value'},
      {key: 'value2'},
      {key: 'value'}
    ];
    expect(uniqueFilter(arrayToFilter)).toEqual([
      {key: 'value'},
      {key: 'value2'}
    ]);
  });

  it('should return unique entries based on object equality for complex objects', function () {
    var arrayToFilter = [
      {key: 'value', other: 'other1'},
      {key: 'value2', other: 'other2'},
      {other: 'other1', key: 'value'}
    ];
    expect(uniqueFilter(arrayToFilter)).toEqual([
      {key: 'value', other: 'other1'},
      {key: 'value2', other: 'other2'}
    ]);
  });

  it('should return unique entries based on the key provided', function () {
    var arrayToFilter = [
      {key: 'value'},
      {key: 'value2'},
      {key: 'value'}
    ];
    expect(uniqueFilter(arrayToFilter, 'key')).toEqual([
      {key: 'value'},
      {key: 'value2'}
    ]);
  });

  it('should return unique entries based on the key provided for complex objects', function () {
    var arrayToFilter = [
      {key: 'value', other: 'other1'},
      {key: 'value2', other: 'other2'},
      {key: 'value', other: 'other3'}
    ];
    expect(uniqueFilter(arrayToFilter, 'key')).toEqual([
      { key: 'value', other: 'other1' },
      { key: 'value2', other: 'other2' }
    ]);
  });

  it('should return unique primitives in arrays', function () {
    expect(uniqueFilter([1, 2, 1, 3])).toEqual([1, 2, 3]);
  });

  it('should work correctly for arrays of mixed elements and object equality', function () {
    expect(uniqueFilter([1, {key: 'value'}, 1, {key: 'value'}, 2, "string", 3])).toEqual([1, {key: 'value'}, 2, "string", 3]);
  });

  it('should work correctly for arrays of mixed elements and a key specified', function () {
    expect(uniqueFilter([1, {key: 'value'}, 1, {key: 'value'}, 2, "string", 3], 'key')).toEqual([1, {key: 'value'}, 2, "string", 3]);
  });

  it('should return unmodified object if not array', function () {
    expect(uniqueFilter('string', 'someKey')).toEqual('string');
  });

  it('should return unmodified array if provided key === false', function () {
    var arrayToFilter = [
      {key: 'value1'},
      {key: 'value2'}
    ];
    expect(uniqueFilter(arrayToFilter, false)).toEqual(arrayToFilter);
  });

});