|
1 | 1 | import { UpdatedDescendantsChange } from '../changes'; |
| 2 | +import { ViewerM2M, ChannelUser, Channel, ContentNode } from '../resources'; |
2 | 3 | import db from 'shared/data/db'; |
3 | 4 | import { CHANGE_TYPES, TABLE_NAMES } from 'shared/data/constants'; |
4 | 5 | import { ContentKindsNames } from 'shared/leUtils/ContentKinds'; |
5 | | -import { ContentNode } from 'shared/data/resources'; |
6 | 6 | import { mockChannelScope, resetMockChannelScope } from 'shared/utils/testing'; |
| 7 | +import client from 'shared/client'; |
| 8 | +import urls from 'shared/urls'; |
7 | 9 |
|
8 | 10 | const CLIENTID = 'test-client-id'; |
9 | 11 |
|
@@ -170,5 +172,53 @@ describe('Resources', () => { |
170 | 172 | expect(change.mods).toEqual(changes); |
171 | 173 | }); |
172 | 174 | }); |
| 175 | + describe('ChannelUser resource', () => { |
| 176 | + const testChannelId = 'test-channel-id'; |
| 177 | + const testUserId = 'test-user-id'; |
| 178 | + |
| 179 | + beforeEach(async () => { |
| 180 | + await db[TABLE_NAMES.VIEWER_M2M].clear(); |
| 181 | + await db[TABLE_NAMES.CHANNEL].clear(); |
| 182 | + jest.spyOn(client, 'delete').mockResolvedValue({}); |
| 183 | + jest.spyOn(Channel.table, 'delete').mockResolvedValue(true); |
| 184 | + jest.spyOn(urls, 'channeluser_remove_self').mockReturnValue(`fake_url_for_${testUserId}`); |
| 185 | + }); |
| 186 | + |
| 187 | + afterEach(() => { |
| 188 | + client.delete.mockRestore(); |
| 189 | + Channel.table.delete.mockRestore(); |
| 190 | + urls.channeluser_remove_self.mockRestore(); |
| 191 | + }); |
| 192 | + |
| 193 | + it('should remove the user from the ViewerM2M table when removeViewer is called', async () => { |
| 194 | + await ViewerM2M.add({ user: testUserId, channel: testChannelId }); |
| 195 | + let viewer = await ViewerM2M.get([testUserId, testChannelId]); |
| 196 | + expect(viewer).toBeTruthy(); |
| 197 | + |
| 198 | + await ChannelUser.removeViewer(testChannelId, testUserId); |
| 199 | + |
| 200 | + viewer = await ViewerM2M.get([testUserId, testChannelId]); |
| 201 | + expect(viewer).toBeUndefined(); |
| 202 | + expect(client.delete).toHaveBeenCalledWith(urls.channeluser_remove_self(testUserId), { |
| 203 | + params: { channel_id: testChannelId }, |
| 204 | + }); |
| 205 | + }); |
| 206 | + |
| 207 | + it('should call Channel.table.delete(channel) when removeViewer is called', async () => { |
| 208 | + await ViewerM2M.add({ user: testUserId, channel: testChannelId }); |
| 209 | + const viewer = await ViewerM2M.get([testUserId, testChannelId]); |
| 210 | + expect(viewer).toBeTruthy(); |
| 211 | + await ChannelUser.removeViewer(testChannelId, testUserId); |
| 212 | + expect(Channel.table.delete).toHaveBeenCalledWith(testChannelId); |
| 213 | + }); |
| 214 | + |
| 215 | + it('should handle error from client.delete when removeViewer is called', async () => { |
| 216 | + jest.spyOn(client, 'delete').mockRejectedValue(new Error('error deleting')); |
| 217 | + await ViewerM2M.add({ user: testUserId, channel: testChannelId }); |
| 218 | + await expect(ChannelUser.removeViewer(testChannelId, testUserId)).rejects.toThrow( |
| 219 | + 'error deleting' |
| 220 | + ); |
| 221 | + }); |
| 222 | + }); |
173 | 223 | }); |
174 | 224 | }); |
0 commit comments