go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/analysis/frontend/ui/src/components/timestamp_info_bar/timestamp_info_bar.test.tsx (about)

     1  // Copyright 2022 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 '@testing-library/jest-dom';
    16  
    17  import {
    18    render,
    19    screen,
    20    waitFor,
    21  } from '@testing-library/react';
    22  
    23  import { createDefaultMockRule } from '@/testing_tools/mocks/rule_mock';
    24  
    25  import TimestampInfoBar from './timestamp_info_bar';
    26  
    27  describe('Test TimestampInfoBar component', () => {
    28    it('when proved with rule, then should render username and timestamps', async () => {
    29      const rule = createDefaultMockRule();
    30      render(<TimestampInfoBar
    31        createUsername={rule.createUser}
    32        createTime={rule.createTime}
    33        updateUsername={rule.lastAuditableUpdateUser}
    34        updateTime={rule.lastAuditableUpdateTime}/>);
    35      await waitFor(() => screen.getByTestId('timestamp-info-bar-create'));
    36  
    37      expect(screen.getByTestId('timestamp-info-bar-create'))
    38          .toHaveTextContent('Created by LUCI Analysis a few seconds ago');
    39      expect(screen.getByTestId('timestamp-info-bar-update'))
    40          .toHaveTextContent('Last modified by user@example.com a few seconds ago');
    41    });
    42  
    43    it('when provided with a google account, then should display name only', async () => {
    44      const rule = createDefaultMockRule();
    45      rule.createUser = 'googler@google.com';
    46      render(<TimestampInfoBar
    47        createUsername={rule.createUser}
    48        createTime={rule.createTime}
    49        updateUsername={rule.lastAuditableUpdateUser}
    50        updateTime={rule.lastAuditableUpdateTime}/>);
    51      await waitFor(() => screen.getByTestId('timestamp-info-bar-create'));
    52  
    53      expect(screen.getByTestId('timestamp-info-bar-create'))
    54          .toHaveTextContent('Created by googler a few seconds ago');
    55      expect(screen.getByTestId('timestamp-info-bar-update'))
    56          .toHaveTextContent('Last modified by user@example.com a few seconds ago');
    57    });
    58  
    59    it('when provided with an external user, then should use full username', async () => {
    60      const rule = createDefaultMockRule();
    61      rule.createUser = 'user@example.com';
    62      render(<TimestampInfoBar
    63        createUsername={rule.createUser}
    64        createTime={rule.createTime}
    65        updateUsername={rule.lastAuditableUpdateUser}
    66        updateTime={rule.lastAuditableUpdateTime}/>);
    67      await waitFor(() => screen.getByTestId('timestamp-info-bar-create'));
    68  
    69      expect(screen.getByTestId('timestamp-info-bar-create'))
    70          .toHaveTextContent('Created by user@example.com a few seconds ago');
    71      expect(screen.getByTestId('timestamp-info-bar-update'))
    72          .toHaveTextContent('Last modified by user@example.com a few seconds ago');
    73    });
    74    it('when provided with no user, then only time should be displayed', async () => {
    75      const rule = createDefaultMockRule();
    76      rule.createUser = '';
    77      rule.lastAuditableUpdateUser = '';
    78      render(<TimestampInfoBar
    79        createUsername={rule.createUser}
    80        createTime={rule.createTime}
    81        updateUsername={rule.lastAuditableUpdateUser}
    82        updateTime={rule.lastAuditableUpdateTime}/>);
    83      await waitFor(() => screen.getByTestId('timestamp-info-bar-create'));
    84  
    85      expect(screen.getByTestId('timestamp-info-bar-create'))
    86          .toHaveTextContent('Created a few seconds ago');
    87      expect(screen.getByTestId('timestamp-info-bar-update'))
    88          .toHaveTextContent('Last modified a few seconds ago');
    89    });
    90  });