From 5557d2e666345caf58e38fcaa5d11a3ef196961f Mon Sep 17 00:00:00 2001 From: Alex Khomenko Date: Wed, 25 May 2022 13:01:40 +0300 Subject: [PATCH] TeamList: Convert test to RTL (#49567) --- .betterer.results | 3 - public/app/features/teams/TeamList.test.tsx | 75 +- public/app/features/teams/TeamList.tsx | 2 +- .../__snapshots__/TeamList.test.tsx.snap | 666 ------------------ 4 files changed, 21 insertions(+), 725 deletions(-) delete mode 100644 public/app/features/teams/__snapshots__/TeamList.test.tsx.snap diff --git a/.betterer.results b/.betterer.results index 7c54b12b30d8..c31ceff38955 100644 --- a/.betterer.results +++ b/.betterer.results @@ -188,9 +188,6 @@ exports[`no enzyme tests`] = { "public/app/features/org/OrgProfile.test.tsx:623809345": [ [0, 19, 13, "RegExp match", "2409514259"] ], - "public/app/features/teams/TeamList.test.tsx:854193970": [ - [0, 19, 13, "RegExp match", "2409514259"] - ], "public/app/features/teams/TeamMemberRow.test.tsx:1649328210": [ [0, 19, 13, "RegExp match", "2409514259"] ], diff --git a/public/app/features/teams/TeamList.test.tsx b/public/app/features/teams/TeamList.test.tsx index 598ecdd3a5c3..9575b0c1ed07 100644 --- a/public/app/features/teams/TeamList.test.tsx +++ b/public/app/features/teams/TeamList.test.tsx @@ -1,4 +1,5 @@ -import { shallow } from 'enzyme'; +import { render, screen, waitFor } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; import React from 'react'; import { mockToolkitActionCreator } from 'test/core/redux/mocks'; @@ -47,35 +48,19 @@ const setup = (propOverrides?: object) => { contextSrv.user = props.signedInUser; - const wrapper = shallow(); - const instance = wrapper.instance() as TeamList; - - return { - wrapper, - instance, - }; + render(); }; -describe('Render', () => { - it('should render component', () => { - const { wrapper } = setup(); - expect(wrapper).toMatchSnapshot(); - }); - +describe('TeamList', () => { it('should render teams table', () => { - const { wrapper } = setup({ - teams: getMultipleMockTeams(5), - teamsCount: 5, - hasFetched: true, - }); - - expect(wrapper).toMatchSnapshot(); + setup({ teams: getMultipleMockTeams(5), teamsCount: 5, hasFetched: true }); + expect(screen.getAllByRole('row')).toHaveLength(6); // 5 teams plus table header row }); describe('when feature toggle editorsCanAdmin is turned on', () => { - describe('and signedin user is not viewer', () => { + describe('and signed in user is not viewer', () => { it('should enable the new team button', () => { - const { wrapper } = setup({ + setup({ teams: getMultipleMockTeams(1), teamsCount: 1, hasFetched: true, @@ -86,13 +71,13 @@ describe('Render', () => { } as User, }); - expect(wrapper).toMatchSnapshot(); + expect(screen.getByRole('link', { name: /new team/i })).not.toHaveStyle('pointer-events: none'); }); }); - describe('and signedin user is a viewer', () => { + describe('and signed in user is a viewer', () => { it('should disable the new team button', () => { - const { wrapper } = setup({ + setup({ teams: getMultipleMockTeams(1), teamsCount: 1, hasFetched: true, @@ -103,39 +88,19 @@ describe('Render', () => { } as User, }); - expect(wrapper).toMatchSnapshot(); + expect(screen.getByRole('link', { name: /new team/i })).toHaveStyle('pointer-events: none'); }); }); }); }); -describe('Life cycle', () => { - it('should call loadTeams', () => { - const { instance } = setup(); - - instance.componentDidMount(); - - expect(instance.props.loadTeams).toHaveBeenCalled(); - }); -}); - -describe('Functions', () => { - describe('Delete team', () => { - it('should call delete team', () => { - const { instance } = setup(); - instance.deleteTeam(getMockTeam()); - - expect(instance.props.deleteTeam).toHaveBeenCalledWith(1); - }); - }); - - describe('on search query change', () => { - it('should call setSearchQuery', () => { - const { instance } = setup(); - - instance.onSearchQueryChange('test'); - - expect(instance.props.setSearchQuery).toHaveBeenCalledWith('test'); - }); +it('should call delete team', async () => { + const mockDelete = jest.fn(); + const mockTeam = getMockTeam(); + setup({ deleteTeam: mockDelete, teams: [mockTeam], teamsCount: 1, hasFetched: true }); + await userEvent.click(screen.getByRole('button', { name: `Delete team ${mockTeam.name}` })); + await userEvent.click(screen.getByRole('button', { name: 'Delete' })); + await waitFor(() => { + expect(mockDelete).toHaveBeenCalledWith(mockTeam.id); }); }); diff --git a/public/app/features/teams/TeamList.tsx b/public/app/features/teams/TeamList.tsx index 99913f21f523..7f2cda0992b0 100644 --- a/public/app/features/teams/TeamList.tsx +++ b/public/app/features/teams/TeamList.tsx @@ -125,7 +125,7 @@ export class TeamList extends PureComponent { )} this.deleteTeam(team)} diff --git a/public/app/features/teams/__snapshots__/TeamList.test.tsx.snap b/public/app/features/teams/__snapshots__/TeamList.test.tsx.snap deleted file mode 100644 index 7e7b2c6d41f6..000000000000 --- a/public/app/features/teams/__snapshots__/TeamList.test.tsx.snap +++ /dev/null @@ -1,666 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`Render should render component 1`] = ` - - - -`; - -exports[`Render should render teams table 1`] = ` - - -
-
- -
- - New Team - -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - Name - - Email - - Members - -
- - Team avatar - - - - test-1 - - - - test-1@test.com - - - - 1 - - - -
- - Team avatar - - - - test-2 - - - - test-2@test.com - - - - 2 - - - -
- - Team avatar - - - - test-3 - - - - test-3@test.com - - - - 3 - - - -
- - Team avatar - - - - test-4 - - - - test-4@test.com - - - - 4 - - - -
- - Team avatar - - - - test-5 - - - - test-5@test.com - - - - 5 - - - -
- - - -
-
-
-
-`; - -exports[`Render when feature toggle editorsCanAdmin is turned on and signedin user is a viewer should disable the new team button 1`] = ` - - -
-
- -
- - New Team - -
-
- - - - - - - - - - - - - - - - - - -
- - Name - - Email - - Members - -
- Team avatar - -
- test-1 -
-
-
- test-1@test.com -
-
-
- 1 -
-
- -
- - - -
-
-
-
-`; - -exports[`Render when feature toggle editorsCanAdmin is turned on and signedin user is not viewer should enable the new team button 1`] = ` - - -
-
- -
- - New Team - -
-
- - - - - - - - - - - - - - - - - - -
- - Name - - Email - - Members - -
- Team avatar - -
- test-1 -
-
-
- test-1@test.com -
-
-
- 1 -
-
- -
- - - -
-
-
-
-`;