TeamList.test.tsx 3.0 KB

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