|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {KeyValuePipe} from '@angular/common'; |
| 10 | +import {EventEmitter, KeyValueDiffers, WrappedValue, ɵdefaultKeyValueDiffers as defaultKeyValueDiffers} from '@angular/core'; |
| 11 | +import {AsyncTestCompleter, beforeEach, describe, expect, inject, it} from '@angular/core/testing/src/testing_internal'; |
| 12 | +import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter'; |
| 13 | +import {browserDetection} from '@angular/platform-browser/testing/src/browser_util'; |
| 14 | + |
| 15 | +import {defaultComparator} from '../../src/pipes/keyvalue_pipe'; |
| 16 | +import {SpyChangeDetectorRef} from '../spies'; |
| 17 | + |
| 18 | +describe('KeyValuePipe', () => { |
| 19 | + it('should return null when given null', () => { |
| 20 | + const pipe = new KeyValuePipe(defaultKeyValueDiffers); |
| 21 | + expect(pipe.transform(null)).toEqual(null); |
| 22 | + }); |
| 23 | + it('should return null when given undefined', () => { |
| 24 | + const pipe = new KeyValuePipe(defaultKeyValueDiffers); |
| 25 | + expect(pipe.transform(undefined as any)).toEqual(null); |
| 26 | + }); |
| 27 | + it('should return null for an unsupported type', () => { |
| 28 | + const pipe = new KeyValuePipe(defaultKeyValueDiffers); |
| 29 | + const fn = () => {}; |
| 30 | + expect(pipe.transform(fn as any)).toEqual(null); |
| 31 | + }); |
| 32 | + describe('object dictionary', () => { |
| 33 | + it('should transform a basic dictionary', () => { |
| 34 | + const pipe = new KeyValuePipe(defaultKeyValueDiffers); |
| 35 | + expect(pipe.transform({1: 2})).toEqual([{key: '1', value: 2}]); |
| 36 | + }); |
| 37 | + it('should order by alpha', () => { |
| 38 | + const pipe = new KeyValuePipe(defaultKeyValueDiffers); |
| 39 | + expect(pipe.transform({'b': 1, 'a': 1})).toEqual([ |
| 40 | + {key: 'a', value: 1}, {key: 'b', value: 1} |
| 41 | + ]); |
| 42 | + }); |
| 43 | + it('should order by numerical', () => { |
| 44 | + const pipe = new KeyValuePipe(defaultKeyValueDiffers); |
| 45 | + expect(pipe.transform({2: 1, 1: 1})).toEqual([{key: '1', value: 1}, {key: '2', value: 1}]); |
| 46 | + }); |
| 47 | + it('should order by numerical and alpha', () => { |
| 48 | + const pipe = new KeyValuePipe(defaultKeyValueDiffers); |
| 49 | + const input = {2: 1, 1: 1, 'b': 1, 0: 1, 3: 1, 'a': 1}; |
| 50 | + expect(pipe.transform(input)).toEqual([ |
| 51 | + {key: '0', value: 1}, {key: '1', value: 1}, {key: '2', value: 1}, {key: '3', value: 1}, |
| 52 | + {key: 'a', value: 1}, {key: 'b', value: 1} |
| 53 | + ]); |
| 54 | + }); |
| 55 | + it('should return the same ref if nothing changes', () => { |
| 56 | + const pipe = new KeyValuePipe(defaultKeyValueDiffers); |
| 57 | + const transform1 = pipe.transform({1: 2}); |
| 58 | + const transform2 = pipe.transform({1: 2}); |
| 59 | + expect(transform1 === transform2).toEqual(true); |
| 60 | + }); |
| 61 | + it('should return a new ref if something changes', () => { |
| 62 | + const pipe = new KeyValuePipe(defaultKeyValueDiffers); |
| 63 | + const transform1 = pipe.transform({1: 2}); |
| 64 | + const transform2 = pipe.transform({1: 3}); |
| 65 | + expect(transform1 !== transform2).toEqual(true); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + describe('Map', () => { |
| 70 | + it('should transform a basic Map', () => { |
| 71 | + const pipe = new KeyValuePipe(defaultKeyValueDiffers); |
| 72 | + expect(pipe.transform(new Map([[1, 2]]))).toEqual([{key: 1, value: 2}]); |
| 73 | + }); |
| 74 | + it('should order by alpha', () => { |
| 75 | + const pipe = new KeyValuePipe(defaultKeyValueDiffers); |
| 76 | + expect(pipe.transform(new Map([['b', 1], ['a', 1]]))).toEqual([ |
| 77 | + {key: 'a', value: 1}, {key: 'b', value: 1} |
| 78 | + ]); |
| 79 | + }); |
| 80 | + it('should order by numerical', () => { |
| 81 | + const pipe = new KeyValuePipe(defaultKeyValueDiffers); |
| 82 | + expect(pipe.transform(new Map([[2, 1], [1, 1]]))).toEqual([ |
| 83 | + {key: 1, value: 1}, {key: 2, value: 1} |
| 84 | + ]); |
| 85 | + }); |
| 86 | + it('should order by numerical and alpha', () => { |
| 87 | + const pipe = new KeyValuePipe(defaultKeyValueDiffers); |
| 88 | + const input = [[2, 1], [1, 1], ['b', 1], [0, 1], [3, 1], ['a', 1]]; |
| 89 | + expect(pipe.transform(new Map(input as any))).toEqual([ |
| 90 | + {key: 0, value: 1}, {key: 1, value: 1}, {key: 2, value: 1}, {key: 3, value: 1}, |
| 91 | + {key: 'a', value: 1}, {key: 'b', value: 1} |
| 92 | + ]); |
| 93 | + }); |
| 94 | + it('should order by complex types with compareFn', () => { |
| 95 | + const pipe = new KeyValuePipe(defaultKeyValueDiffers); |
| 96 | + const input = new Map([[{id: 1}, 1], [{id: 0}, 1]]); |
| 97 | + expect(pipe.transform<{id: number}, number>(input, (a, b) => a.key.id > b.key.id ? 1 : -1)) |
| 98 | + .toEqual([ |
| 99 | + {key: {id: 0}, value: 1}, |
| 100 | + {key: {id: 1}, value: 1}, |
| 101 | + ]); |
| 102 | + }); |
| 103 | + it('should return the same ref if nothing changes', () => { |
| 104 | + const pipe = new KeyValuePipe(defaultKeyValueDiffers); |
| 105 | + const transform1 = pipe.transform(new Map([[1, 2]])); |
| 106 | + const transform2 = pipe.transform(new Map([[1, 2]])); |
| 107 | + expect(transform1 === transform2).toEqual(true); |
| 108 | + }); |
| 109 | + it('should return a new ref if something changes', () => { |
| 110 | + const pipe = new KeyValuePipe(defaultKeyValueDiffers); |
| 111 | + const transform1 = pipe.transform(new Map([[1, 2]])); |
| 112 | + const transform2 = pipe.transform(new Map([[1, 3]])); |
| 113 | + expect(transform1 !== transform2).toEqual(true); |
| 114 | + }); |
| 115 | + }); |
| 116 | +}); |
| 117 | + |
| 118 | +describe('defaultComparator', () => { |
| 119 | + it('should remain the same order when keys are equal', () => { |
| 120 | + const key = 1; |
| 121 | + const values = [{key, value: 2}, {key, value: 1}]; |
| 122 | + expect(values.sort(defaultComparator)).toEqual(values); |
| 123 | + }); |
| 124 | + it('should sort undefined keys to the end', () => { |
| 125 | + const values = [{key: 3, value: 1}, {key: undefined, value: 3}, {key: 1, value: 2}]; |
| 126 | + expect(values.sort(defaultComparator)).toEqual([ |
| 127 | + {key: 1, value: 2}, {key: 3, value: 1}, {key: undefined, value: 3} |
| 128 | + ]); |
| 129 | + }); |
| 130 | + it('should sort null keys to the end', () => { |
| 131 | + const values = [{key: 3, value: 1}, {key: null, value: 3}, {key: 1, value: 2}]; |
| 132 | + expect(values.sort(defaultComparator)).toEqual([ |
| 133 | + {key: 1, value: 2}, {key: 3, value: 1}, {key: null, value: 3} |
| 134 | + ]); |
| 135 | + }); |
| 136 | + it('should sort strings in alpha ascending', () => { |
| 137 | + const values = [{key: 'b', value: 1}, {key: 'a', value: 3}]; |
| 138 | + expect(values.sort(defaultComparator)).toEqual([{key: 'a', value: 3}, {key: 'b', value: 1}]); |
| 139 | + }); |
| 140 | + it('should sort numbers in numerical ascending', () => { |
| 141 | + const values = [{key: 2, value: 1}, {key: 1, value: 3}]; |
| 142 | + expect(values.sort(defaultComparator)).toEqual([{key: 1, value: 3}, {key: 2, value: 1}]); |
| 143 | + }); |
| 144 | + it('should sort boolean in false (0) -> true (1)', () => { |
| 145 | + const values = [{key: true, value: 3}, {key: false, value: 1}]; |
| 146 | + expect(values.sort(defaultComparator)).toEqual([{key: false, value: 1}, {key: true, value: 3}]); |
| 147 | + }); |
| 148 | + it('should sort numbers as strings in numerical ascending', () => { |
| 149 | + const values = [{key: '2', value: 1}, {key: 1, value: 3}]; |
| 150 | + expect(values.sort(defaultComparator)).toEqual([{key: 1, value: 3}, {key: '2', value: 1}]); |
| 151 | + }); |
| 152 | +}); |
0 commit comments