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