session.spec.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. import { CurrentUserDocument } from '@shared/graphql/queries/currentUser.api'
  3. import { CurrentUserUpdatesDocument } from '@shared/graphql/subscriptions/currentUserUpdates.api'
  4. import {
  5. mockGraphQLApi,
  6. mockGraphQLSubscription,
  7. } from '@tests/support/mock-graphql-api'
  8. import { createPinia, setActivePinia } from 'pinia'
  9. import { useSessionStore } from '../session'
  10. const userData = {
  11. __typename: 'User',
  12. id: '123456789',
  13. internalId: 1,
  14. firstname: 'John',
  15. lastname: 'Doe',
  16. fullname: 'John Doe',
  17. email: 'zammad@example.com',
  18. image: 'c2715a3e92c7e375b8c212d25d431e2a',
  19. preferences: {
  20. locale: 'de-de',
  21. },
  22. objectAttributeValues: [],
  23. organization: {
  24. __typename: 'Organization',
  25. id: '234241',
  26. internalId: 1,
  27. name: 'Zammad Foundation',
  28. objectAttributeValues: [],
  29. active: true,
  30. },
  31. permissions: {
  32. __typename: 'Permission',
  33. names: ['admin'],
  34. },
  35. hasSecondaryOrganizations: false,
  36. }
  37. describe('Session Store', () => {
  38. beforeEach(() => {
  39. setActivePinia(createPinia())
  40. })
  41. it('is empty by default', () => {
  42. const session = useSessionStore()
  43. expect(session.id).toBe(null)
  44. expect(session.user).toBe(null)
  45. })
  46. it('get current user and check on user update subscription', async () => {
  47. const session = useSessionStore()
  48. mockGraphQLApi(CurrentUserDocument).willResolve({
  49. currentUser: userData,
  50. })
  51. const userUpdateSubscription = mockGraphQLSubscription(
  52. CurrentUserUpdatesDocument,
  53. )
  54. await session.getCurrentUser()
  55. expect(session.user).toEqual(userData)
  56. const updatedUserData = {
  57. ...userData,
  58. firstname: 'Jane',
  59. lastname: 'Doe',
  60. fullname: 'Jane Doe',
  61. }
  62. await userUpdateSubscription.next({
  63. data: {
  64. userUpdates: {
  65. __typename: 'UserUpdatesPayload',
  66. user: updatedUserData,
  67. },
  68. },
  69. })
  70. expect(session.user).toEqual(updatedUserData)
  71. })
  72. })