TeamList.test.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import { shallow } from 'enzyme';
  2. import React from 'react';
  3. import { mockToolkitActionCreator } from 'test/core/redux/mocks';
  4. import { NavModel } from '@grafana/data';
  5. import { contextSrv, User } from 'app/core/services/context_srv';
  6. import { OrgRole, Team } from '../../types';
  7. import { Props, TeamList } from './TeamList';
  8. import { getMockTeam, getMultipleMockTeams } from './__mocks__/teamMocks';
  9. import { setSearchQuery, setTeamsSearchPage } from './state/reducers';
  10. jest.mock('app/core/config', () => {
  11. return {
  12. featureToggles: { accesscontrol: false },
  13. };
  14. });
  15. const setup = (propOverrides?: object) => {
  16. const props: Props = {
  17. navModel: {
  18. main: {
  19. text: 'Configuration',
  20. },
  21. node: {
  22. text: 'Team List',
  23. },
  24. } as NavModel,
  25. teams: [] as Team[],
  26. loadTeams: jest.fn(),
  27. deleteTeam: jest.fn(),
  28. setSearchQuery: mockToolkitActionCreator(setSearchQuery),
  29. setTeamsSearchPage: mockToolkitActionCreator(setTeamsSearchPage),
  30. searchQuery: '',
  31. searchPage: 1,
  32. teamsCount: 0,
  33. hasFetched: false,
  34. editorsCanAdmin: false,
  35. signedInUser: {
  36. id: 1,
  37. orgRole: OrgRole.Viewer,
  38. } as User,
  39. };
  40. Object.assign(props, propOverrides);
  41. contextSrv.user = props.signedInUser;
  42. const wrapper = shallow(<TeamList {...props} />);
  43. const instance = wrapper.instance() as TeamList;
  44. return {
  45. wrapper,
  46. instance,
  47. };
  48. };
  49. describe('Render', () => {
  50. it('should render component', () => {
  51. const { wrapper } = setup();
  52. expect(wrapper).toMatchSnapshot();
  53. });
  54. it('should render teams table', () => {
  55. const { wrapper } = setup({
  56. teams: getMultipleMockTeams(5),
  57. teamsCount: 5,
  58. hasFetched: true,
  59. });
  60. expect(wrapper).toMatchSnapshot();
  61. });
  62. describe('when feature toggle editorsCanAdmin is turned on', () => {
  63. describe('and signedin user is not viewer', () => {
  64. it('should enable the new team button', () => {
  65. const { wrapper } = setup({
  66. teams: getMultipleMockTeams(1),
  67. teamsCount: 1,
  68. hasFetched: true,
  69. editorsCanAdmin: true,
  70. signedInUser: {
  71. id: 1,
  72. orgRole: OrgRole.Editor,
  73. } as User,
  74. });
  75. expect(wrapper).toMatchSnapshot();
  76. });
  77. });
  78. describe('and signedin user is a viewer', () => {
  79. it('should disable the new team button', () => {
  80. const { wrapper } = setup({
  81. teams: getMultipleMockTeams(1),
  82. teamsCount: 1,
  83. hasFetched: true,
  84. editorsCanAdmin: true,
  85. signedInUser: {
  86. id: 1,
  87. orgRole: OrgRole.Viewer,
  88. } as User,
  89. });
  90. expect(wrapper).toMatchSnapshot();
  91. });
  92. });
  93. });
  94. });
  95. describe('Life cycle', () => {
  96. it('should call loadTeams', () => {
  97. const { instance } = setup();
  98. instance.componentDidMount();
  99. expect(instance.props.loadTeams).toHaveBeenCalled();
  100. });
  101. });
  102. describe('Functions', () => {
  103. describe('Delete team', () => {
  104. it('should call delete team', () => {
  105. const { instance } = setup();
  106. instance.deleteTeam(getMockTeam());
  107. expect(instance.props.deleteTeam).toHaveBeenCalledWith(1);
  108. });
  109. });
  110. describe('on search query change', () => {
  111. it('should call setSearchQuery', () => {
  112. const { instance } = setup();
  113. instance.onSearchQueryChange('test');
  114. expect(instance.props.setSearchQuery).toHaveBeenCalledWith('test');
  115. });
  116. });
  117. });