go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/milo/ui/src/common/components/property_viewer/property_viewer.test.tsx (about)

     1  // Copyright 2023 The LUCI Authors.
     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 { cleanup, render } from '@testing-library/react';
    16  import CodeMirror from 'codemirror';
    17  import { destroy } from 'mobx-state-tree';
    18  
    19  import {
    20    PropertyViewerConfig,
    21    PropertyViewerConfigInstance,
    22  } from '@/common/store/user_config/build_config';
    23  
    24  import { PropertyViewer } from './property_viewer';
    25  
    26  const TEST_PROPERTIES = {
    27    key1: {
    28      child1: 'value1',
    29    },
    30    key2: {
    31      child2: 'value2',
    32    },
    33  };
    34  
    35  const TEST_PROPERTIES_LINES = JSON.stringify(
    36    TEST_PROPERTIES,
    37    undefined,
    38    2,
    39  ).split('\n');
    40  
    41  // jsdom doesn't allow us to unit test a codemirror editor.
    42  // eslint-disable-next-line jest/no-disabled-tests
    43  describe.skip('PropertyViewer', () => {
    44    let store: PropertyViewerConfigInstance;
    45  
    46    beforeEach(() => {
    47      jest.useFakeTimers();
    48      store = PropertyViewerConfig.create({});
    49    });
    50  
    51    afterEach(() => {
    52      cleanup();
    53      destroy(store);
    54      jest.useRealTimers();
    55    });
    56  
    57    test('e2e', async () => {
    58      store.setFolded('  "key1": {', true);
    59      store.setFolded('  "key3": {', true);
    60  
    61      await jest.advanceTimersByTimeAsync(1000 * 60 * 60);
    62      const beforeRender = jest.now();
    63  
    64      // Renders properties.
    65      await jest.advanceTimersByTimeAsync(1000 * 60 * 60);
    66  
    67      let editor: CodeMirror.Editor;
    68      render(
    69        <PropertyViewer
    70          properties={TEST_PROPERTIES}
    71          config={store}
    72          onInit={(e) => {
    73            editor = e;
    74          }}
    75        />,
    76      );
    77      await jest.runAllTimersAsync();
    78  
    79      expect(store.isFolded('  "key1": {')).toBeTruthy();
    80      expect(store.isFolded('  "key2": {')).toBeFalsy();
    81      expect(store.isFolded('  "key3": {')).toBeTruthy();
    82  
    83      store.deleteStaleKeys(new Date(beforeRender));
    84      // Rendered key's fold state should be refreshed.
    85      expect(store.isFolded('  "key1": {')).toBeTruthy();
    86      // Un-rendered key's fold state should not be refreshed.
    87      // So stale keys won't take up memory/disk space.
    88      expect(store.isFolded('  "key2": {')).toBeFalsy();
    89      expect(store.isFolded('  "key3": {')).toBeFalsy();
    90  
    91      await jest.runAllTimersAsync();
    92  
    93      // Unfold key1.
    94      editor!.foldCode(
    95        TEST_PROPERTIES_LINES.indexOf('  "key1": {'),
    96        undefined,
    97        'unfold',
    98      );
    99      await jest.runAllTimersAsync();
   100  
   101      expect(store.isFolded('  "key1": {')).toBeFalsy();
   102      expect(store.isFolded('  "key2": {')).toBeFalsy();
   103      expect(store.isFolded('  "key3": {')).toBeFalsy();
   104  
   105      // Fold key2.
   106      editor!.foldCode(
   107        TEST_PROPERTIES_LINES.indexOf('  "key2": {'),
   108        undefined,
   109        'fold',
   110      );
   111      await jest.runAllTimersAsync();
   112  
   113      expect(store.isFolded('  "key1": {')).toBeFalsy();
   114      expect(store.isFolded('  "key2": {')).toBeTruthy();
   115      expect(store.isFolded('  "key3": {')).toBeFalsy();
   116    });
   117  });