vitess.io/vitess@v0.16.2/web/vtadmin/src/components/routes/keyspace/Advanced.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 { rest } from 'msw';
    18  import { setupServer } from 'msw/node';
    19  import { render, screen, waitFor, within } from '@testing-library/react';
    20  import userEvent from '@testing-library/user-event';
    21  import { QueryClient, QueryClientProvider } from 'react-query';
    22  import { Advanced } from './Advanced';
    23  import { vtadmin } from '../../../proto/vtadmin';
    24  
    25  const ORIGINAL_PROCESS_ENV = process.env;
    26  const TEST_PROCESS_ENV = {
    27      ...process.env,
    28      REACT_APP_VTADMIN_API_ADDRESS: '',
    29  };
    30  
    31  describe('Advanced keyspace actions', () => {
    32      const keyspace: vtadmin.IKeyspace = {
    33          cluster: { id: 'some-cluster', name: 'some-cluster' },
    34          keyspace: {
    35              name: 'some-keyspace',
    36          },
    37      };
    38  
    39      const server = setupServer(
    40          rest.get('/api/keyspace/:clusterID/:keyspace', (req, res, ctx) => {
    41              return res(ctx.json({ ok: true, result: keyspace }));
    42          }),
    43          rest.put('/api/schemas/reload', (req, res, ctx) => {
    44              return res(ctx.json({ ok: true }));
    45          })
    46      );
    47  
    48      const queryClient = new QueryClient({
    49          defaultOptions: { queries: { retry: false } },
    50      });
    51  
    52      beforeAll(() => {
    53          process.env = { ...TEST_PROCESS_ENV } as NodeJS.ProcessEnv;
    54          server.listen();
    55      });
    56  
    57      beforeEach(() => {
    58          process.env = { ...TEST_PROCESS_ENV } as NodeJS.ProcessEnv;
    59          jest.clearAllMocks();
    60      });
    61  
    62      afterAll(() => {
    63          process.env = { ...ORIGINAL_PROCESS_ENV };
    64          server.close();
    65      });
    66  
    67      describe('Reload Schema', () => {
    68          it('reloads the schema', async () => {
    69              jest.spyOn(global, 'fetch');
    70  
    71              render(
    72                  <QueryClientProvider client={queryClient}>
    73                      <Advanced clusterID="some-cluster" name="some-keyspace" />
    74                  </QueryClientProvider>
    75              );
    76  
    77              expect(screen.getByText('Loading...')).not.toBeNull();
    78  
    79              await waitFor(async () => {
    80                  expect(screen.queryByText('Loading...')).toBeNull();
    81              });
    82  
    83              expect(global.fetch).toHaveBeenCalledTimes(1);
    84              expect(global.fetch).toHaveBeenCalledWith(`/api/keyspace/some-cluster/some-keyspace`, {
    85                  credentials: undefined,
    86              });
    87  
    88              jest.clearAllMocks();
    89  
    90              const container = screen.getByTitle('Reload Schema');
    91              const button = within(container).getByRole('button');
    92              expect(button).not.toHaveAttribute('disabled');
    93  
    94              const user = userEvent.setup();
    95              await user.click(button);
    96              await waitFor(() => {
    97                  expect(global.fetch).toHaveBeenCalledTimes(1);
    98              });
    99  
   100              expect(global.fetch).toHaveBeenCalledWith(
   101                  `/api/schemas/reload?cluster=some-cluster&keyspace=some-keyspace`,
   102                  {
   103                      credentials: undefined,
   104                      method: 'put',
   105                  }
   106              );
   107          });
   108      });
   109  });