TeamList.test.tsx 3.0 KB

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