vitess.io/vitess@v0.16.2/web/vtadmin/src/components/ReadOnlyGate.test.tsx (about)

     1  /**
     2   * Copyright 2022 The Vitess Authors.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  import { render, screen } from '@testing-library/react';
    18  import { ReadOnlyGate } from './ReadOnlyGate';
    19  
    20  // Preserve process.env to restore its original values after each test runs.
    21  const ORIGINAL_PROCESS_ENV = { ...process.env };
    22  
    23  describe('ReadOnlyGate', () => {
    24      afterEach(() => {
    25          process.env = ORIGINAL_PROCESS_ENV;
    26      });
    27  
    28      it('hides children when in read-only mode', () => {
    29          (process as any).env.REACT_APP_READONLY_MODE = 'true';
    30  
    31          render(
    32              <ReadOnlyGate>
    33                  <div data-testid="child">🌶🌶🌶</div>
    34              </ReadOnlyGate>
    35          );
    36  
    37          const child = screen.queryByTestId('child');
    38          expect(child).toBeNull();
    39      });
    40  
    41      it('shows children when not in read-only mode', () => {
    42          render(
    43              <ReadOnlyGate>
    44                  <div data-testid="child">🌶🌶🌶</div>
    45              </ReadOnlyGate>
    46          );
    47  
    48          const child = screen.queryByTestId('child');
    49          expect(child).not.toBeNull();
    50          expect(child).toHaveTextContent('🌶🌶🌶');
    51      });
    52  });