about summary refs log tree commit diff
path: root/__tests__/state/models/me.test.ts
blob: b69e4347736d6bca43abbcd0455b0f7417b7e2a1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import {RootStoreModel} from '../../../src/state/models/root-store'
import {MeModel} from '../../../src/state/models/me'
import {NotificationsViewModel} from './../../../src/state/models/notifications-view'
import {sessionClient, SessionServiceClient} from '@atproto/api'
import {DEFAULT_SERVICE} from './../../../src/state/index'

describe('MeModel', () => {
  let rootStore: RootStoreModel
  let meModel: MeModel

  beforeEach(() => {
    const api = sessionClient.service(DEFAULT_SERVICE) as SessionServiceClient
    rootStore = new RootStoreModel(api)
    meModel = new MeModel(rootStore)
  })

  afterAll(() => {
    jest.clearAllMocks()
  })

  it('should clear() correctly', () => {
    meModel.did = '123'
    meModel.handle = 'handle'
    meModel.displayName = 'John Doe'
    meModel.description = 'description'
    meModel.avatar = 'avatar'
    meModel.notificationCount = 1
    meModel.clear()
    expect(meModel.did).toEqual('')
    expect(meModel.handle).toEqual('')
    expect(meModel.displayName).toEqual('')
    expect(meModel.description).toEqual('')
    expect(meModel.avatar).toEqual('')
    expect(meModel.notificationCount).toEqual(0)
  })

  it('should hydrate() successfully with valid properties', () => {
    meModel.hydrate({
      did: '123',
      handle: 'handle',
      displayName: 'John Doe',
      description: 'description',
      avatar: 'avatar',
    })
    expect(meModel.did).toEqual('123')
    expect(meModel.handle).toEqual('handle')
    expect(meModel.displayName).toEqual('John Doe')
    expect(meModel.description).toEqual('description')
    expect(meModel.avatar).toEqual('avatar')
  })

  it('should not hydrate() with invalid properties', () => {
    meModel.hydrate({
      did: '',
      handle: 'handle',
      displayName: 'John Doe',
      description: 'description',
      avatar: 'avatar',
    })
    expect(meModel.did).toEqual('')
    expect(meModel.handle).toEqual('')
    expect(meModel.displayName).toEqual('')
    expect(meModel.description).toEqual('')
    expect(meModel.avatar).toEqual('')

    meModel.hydrate({
      did: '123',
      displayName: 'John Doe',
      description: 'description',
      avatar: 'avatar',
    })
    expect(meModel.did).toEqual('')
    expect(meModel.handle).toEqual('')
    expect(meModel.displayName).toEqual('')
    expect(meModel.description).toEqual('')
    expect(meModel.avatar).toEqual('')
  })

  it('should load() successfully', async () => {
    jest
      .spyOn(rootStore.api.app.bsky.actor, 'getProfile')
      .mockImplementationOnce((): Promise<any> => {
        return Promise.resolve({
          data: {
            displayName: 'John Doe',
            description: 'description',
            avatar: 'avatar',
          },
        })
      })
    rootStore.session.data = {
      did: '123',
      handle: 'handle',
      service: 'test service',
      accessJwt: 'test token',
      refreshJwt: 'test token',
    }
    await meModel.load()
    expect(meModel.did).toEqual('123')
    expect(meModel.handle).toEqual('handle')
    expect(meModel.displayName).toEqual('John Doe')
    expect(meModel.description).toEqual('description')
    expect(meModel.avatar).toEqual('avatar')
  })

  it('should load() successfully without profile data', async () => {
    jest
      .spyOn(rootStore.api.app.bsky.actor, 'getProfile')
      .mockImplementationOnce((): Promise<any> => {
        return Promise.resolve({
          data: null,
        })
      })
    rootStore.session.data = {
      did: '123',
      handle: 'handle',
      service: 'test service',
      accessJwt: 'test token',
      refreshJwt: 'test token',
    }
    await meModel.load()
    expect(meModel.did).toEqual('123')
    expect(meModel.handle).toEqual('handle')
    expect(meModel.displayName).toEqual('')
    expect(meModel.description).toEqual('')
    expect(meModel.avatar).toEqual('')
  })

  it('should load() to nothing when no session', async () => {
    rootStore.session.data = null
    await meModel.load()
    expect(meModel.did).toEqual('')
    expect(meModel.handle).toEqual('')
    expect(meModel.displayName).toEqual('')
    expect(meModel.description).toEqual('')
    expect(meModel.avatar).toEqual('')
    expect(meModel.notificationCount).toEqual(0)
  })

  it('should serialize() key information', () => {
    meModel.did = '123'
    meModel.handle = 'handle'
    meModel.displayName = 'John Doe'
    meModel.description = 'description'
    meModel.avatar = 'avatar'

    expect(meModel.serialize()).toEqual({
      did: '123',
      handle: 'handle',
      displayName: 'John Doe',
      description: 'description',
      avatar: 'avatar',
    })
  })

  it('should clearNotificationCount() successfully', () => {
    meModel.clearNotificationCount()
    expect(meModel.notificationCount).toBe(0)
  })

  it('should update notifs count with fetchStateUpdate()', async () => {
    meModel.notifications = {
      refresh: jest.fn().mockResolvedValue({}),
    } as unknown as NotificationsViewModel

    jest
      .spyOn(rootStore.api.app.bsky.notification, 'getCount')
      .mockImplementationOnce((): Promise<any> => {
        return Promise.resolve({
          data: {
            count: 1,
          },
        })
      })

    await meModel.fetchNotifications()
    expect(meModel.notificationCount).toBe(1)
    expect(meModel.notifications.refresh).toHaveBeenCalled()
  })
})