|
| 1 | +/* Copyright 2023 The TensorFlow Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +==============================================================================*/ |
| 15 | +import {TestBed} from '@angular/core/testing'; |
| 16 | +import {CardInteractionsDataSource} from './card_interactions_data_source'; |
| 17 | +import {PluginType} from '../internal_types'; |
| 18 | + |
| 19 | +describe('CardInteractionsDataSource Test', () => { |
| 20 | + let mockStorage: Record<string, string>; |
| 21 | + let dataSource: CardInteractionsDataSource; |
| 22 | + |
| 23 | + beforeEach(async () => { |
| 24 | + await TestBed.configureTestingModule({ |
| 25 | + providers: [CardInteractionsDataSource], |
| 26 | + }); |
| 27 | + |
| 28 | + dataSource = TestBed.inject(CardInteractionsDataSource); |
| 29 | + |
| 30 | + mockStorage = {}; |
| 31 | + spyOn(window.localStorage, 'setItem').and.callFake( |
| 32 | + (key: string, value: string) => { |
| 33 | + if (key !== 'tb-card-interactions') { |
| 34 | + throw new Error('incorrect key used'); |
| 35 | + } |
| 36 | + |
| 37 | + mockStorage[key] = value; |
| 38 | + } |
| 39 | + ); |
| 40 | + |
| 41 | + spyOn(window.localStorage, 'getItem').and.callFake((key: string) => { |
| 42 | + if (key !== 'tb-card-interactions') { |
| 43 | + throw new Error('incorrect key used'); |
| 44 | + } |
| 45 | + |
| 46 | + return mockStorage[key]; |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + describe('saveCardInteractions', () => { |
| 51 | + it('only saves 10 pins', () => { |
| 52 | + dataSource.saveCardInteractions({ |
| 53 | + clicks: [], |
| 54 | + tagFilters: [], |
| 55 | + pins: Array.from({length: 12}).map((_, index) => ({ |
| 56 | + cardId: `card-${index}`, |
| 57 | + runId: null, |
| 58 | + tag: 'foo', |
| 59 | + plugin: PluginType.SCALARS, |
| 60 | + })), |
| 61 | + }); |
| 62 | + |
| 63 | + expect(dataSource.getCardInteractions().pins.length).toEqual(10); |
| 64 | + }); |
| 65 | + |
| 66 | + it('only saves 10 clicks', () => { |
| 67 | + dataSource.saveCardInteractions({ |
| 68 | + pins: [], |
| 69 | + tagFilters: [], |
| 70 | + clicks: Array.from({length: 12}).map((_, index) => ({ |
| 71 | + cardId: `card-${index}`, |
| 72 | + runId: null, |
| 73 | + tag: 'foo', |
| 74 | + plugin: PluginType.SCALARS, |
| 75 | + })), |
| 76 | + }); |
| 77 | + |
| 78 | + expect(dataSource.getCardInteractions().clicks.length).toEqual(10); |
| 79 | + }); |
| 80 | + |
| 81 | + it('only saves 10 tagFilgers', () => { |
| 82 | + dataSource.saveCardInteractions({ |
| 83 | + clicks: [], |
| 84 | + tagFilters: Array.from({length: 12}).map((_, index) => |
| 85 | + index.toString() |
| 86 | + ), |
| 87 | + pins: [], |
| 88 | + }); |
| 89 | + |
| 90 | + expect(dataSource.getCardInteractions().tagFilters.length).toEqual(10); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + describe('getCardInteractions', () => { |
| 95 | + it('returns all default state when key is not set', () => { |
| 96 | + expect(dataSource.getCardInteractions()).toEqual({ |
| 97 | + tagFilters: [], |
| 98 | + pins: [], |
| 99 | + clicks: [], |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + it('returns previously written value', () => { |
| 104 | + dataSource.saveCardInteractions({ |
| 105 | + tagFilters: ['foo'], |
| 106 | + clicks: [ |
| 107 | + {cardId: '1', runId: null, tag: 'foo', plugin: PluginType.SCALARS}, |
| 108 | + ], |
| 109 | + pins: [ |
| 110 | + {cardId: '2', runId: null, tag: 'bar', plugin: PluginType.SCALARS}, |
| 111 | + ], |
| 112 | + }); |
| 113 | + |
| 114 | + expect(dataSource.getCardInteractions()).toEqual({ |
| 115 | + tagFilters: ['foo'], |
| 116 | + clicks: [ |
| 117 | + {cardId: '1', runId: null, tag: 'foo', plugin: PluginType.SCALARS}, |
| 118 | + ], |
| 119 | + pins: [ |
| 120 | + {cardId: '2', runId: null, tag: 'bar', plugin: PluginType.SCALARS}, |
| 121 | + ], |
| 122 | + }); |
| 123 | + }); |
| 124 | + }); |
| 125 | +}); |
0 commit comments