Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

users-test.tsx 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. /* eslint-disable sonarjs/no-duplicate-string */
  21. import { mockCurrentUser, mockLoggedInUser, mockUser } from '../../helpers/testMocks';
  22. import reducer, {
  23. getCurrentUser,
  24. getCurrentUserSetting,
  25. getUserByLogin,
  26. getUsersByLogins,
  27. receiveCurrentUser,
  28. setCurrentUserSettingAction,
  29. setHomePageAction,
  30. skipOnboardingAction,
  31. State
  32. } from '../users';
  33. describe('reducer and actions', () => {
  34. it('should allow to receive the current user', () => {
  35. const initialState: State = createState();
  36. const currentUser = mockCurrentUser();
  37. const newState = reducer(initialState, receiveCurrentUser(currentUser));
  38. expect(newState).toEqual(createState({ currentUser }));
  39. });
  40. it('should allow to skip the onboarding tutorials', () => {
  41. const currentUser = mockLoggedInUser({ showOnboardingTutorial: true });
  42. const initialState: State = createState({ currentUser });
  43. const newState = reducer(initialState, skipOnboardingAction());
  44. expect(newState).toEqual(
  45. createState({ currentUser: { ...currentUser, showOnboardingTutorial: false } })
  46. );
  47. });
  48. it('should allow to set the homepage', () => {
  49. const homepage: T.HomePage = { type: 'PROJECTS' };
  50. const currentUser = mockLoggedInUser({ homepage: undefined });
  51. const initialState: State = createState({ currentUser });
  52. const newState = reducer(initialState, setHomePageAction(homepage));
  53. expect(newState).toEqual(
  54. createState({ currentUser: { ...currentUser, homepage } as T.LoggedInUser })
  55. );
  56. });
  57. it('should allow to set a user setting', () => {
  58. const setting1: T.CurrentUserSetting = { key: 'notifications.optOut', value: '1' };
  59. const setting2: T.CurrentUserSetting = { key: 'notifications.readDate', value: '2' };
  60. const setting3: T.CurrentUserSetting = { key: 'notifications.optOut', value: '2' };
  61. const currentUser = mockLoggedInUser();
  62. const initialState: State = createState({ currentUser });
  63. const newState = reducer(initialState, setCurrentUserSettingAction(setting1));
  64. expect(newState).toEqual(
  65. createState({ currentUser: { ...currentUser, settings: [setting1] } as T.LoggedInUser })
  66. );
  67. const newerState = reducer(newState, setCurrentUserSettingAction(setting2));
  68. expect(newerState).toEqual(
  69. createState({
  70. currentUser: { ...currentUser, settings: [setting1, setting2] } as T.LoggedInUser
  71. })
  72. );
  73. const newestState = reducer(newerState, setCurrentUserSettingAction(setting3));
  74. expect(newestState).toEqual(
  75. createState({
  76. currentUser: { ...currentUser, settings: [setting3, setting2] } as T.LoggedInUser
  77. })
  78. );
  79. });
  80. });
  81. describe('getters', () => {
  82. const currentUser = mockLoggedInUser({ settings: [{ key: 'notifications.optOut', value: '1' }] });
  83. const jane = mockUser({ login: 'jane', name: 'Jane Doe' });
  84. const john = mockUser({ login: 'john' });
  85. const state = createState({ currentUser, usersByLogin: { jane, john } });
  86. test('getCurrentUser', () => {
  87. expect(getCurrentUser(state)).toBe(currentUser);
  88. });
  89. test('getCurrentUserSetting', () => {
  90. expect(getCurrentUserSetting(state, 'notifications.optOut')).toBe('1');
  91. expect(getCurrentUserSetting(state, 'notifications.readDate')).toBeUndefined();
  92. });
  93. test('getUserByLogin', () => {
  94. expect(getUserByLogin(state, 'jane')).toBe(jane);
  95. expect(getUserByLogin(state, 'steve')).toBeUndefined();
  96. });
  97. test('getUsersByLogins', () => {
  98. expect(getUsersByLogins(state, ['jane', 'john'])).toEqual([jane, john]);
  99. });
  100. });
  101. function createState(overrides: Partial<State> = {}): State {
  102. return { usersByLogin: {}, userLogins: [], currentUser: mockCurrentUser(), ...overrides };
  103. }