1// Copyright 2024 The Pigweed Authors 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); you may not 4// use this file except in compliance with the License. You may obtain a copy of 5// the License at 6// 7// https://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, WITHOUT 11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12// License for the specific language governing permissions and limitations under 13// the License. 14 15import { expect, assert } from '@open-wc/testing'; 16import { LocalStateStorage, StateService } from '../src/shared/state'; 17import { NodeType, Orientation, ViewNode } from '../src/shared/view-node'; 18 19describe('state', () => { 20 const mockColumnData = [ 21 { 22 fieldName: 'test', 23 characterLength: 0, 24 manualWidth: null, 25 isVisible: false, 26 }, 27 { 28 fieldName: 'foo', 29 characterLength: 0, 30 manualWidth: null, 31 isVisible: true, 32 }, 33 { 34 fieldName: 'bar', 35 characterLength: 0, 36 manualWidth: null, 37 isVisible: false, 38 }, 39 ]; 40 41 const mockState = { 42 rootNode: new ViewNode({ 43 type: NodeType.Split, 44 orientation: Orientation.Horizontal, 45 children: [ 46 new ViewNode({ 47 searchText: 'hello', 48 logViewId: 'child-node-1', 49 type: NodeType.View, 50 columnData: mockColumnData, 51 viewTitle: 'Child Node 1', 52 wordWrap: true, 53 }), 54 new ViewNode({ 55 searchText: 'world', 56 logViewId: 'child-node-2', 57 type: NodeType.View, 58 columnData: mockColumnData, 59 viewTitle: 'Child Node 2', 60 wordWrap: false, 61 }), 62 ], 63 }), 64 }; 65 66 const stateService = new StateService(new LocalStateStorage()); 67 stateService.saveState(mockState); 68 69 describe('state service', () => { 70 it('loads a stored state object', () => { 71 const loadedState = stateService.loadState(); 72 expect(loadedState).to.have.property('rootNode'); 73 assert.lengthOf(loadedState.rootNode.children, 2); 74 }); 75 76 it('sets log view properties correctly', () => { 77 const loadedState = stateService.loadState(); 78 const childNode1 = loadedState.rootNode.children[0]; 79 const childNode2 = loadedState.rootNode.children[1]; 80 81 expect(childNode1.logViewState.viewTitle).to.equal('Child Node 1'); 82 expect(childNode1.logViewState.searchText).to.equal('hello'); 83 expect(childNode1.logViewState.wordWrap).to.equal(true); 84 expect(childNode2.logViewState.viewTitle).to.equal('Child Node 2'); 85 expect(childNode2.logViewState.searchText).to.equal('world'); 86 expect(childNode2.logViewState.wordWrap).to.equal(false); 87 }); 88 }); 89}); 90