|
| 1 | +import * as faker from 'faker'; |
| 2 | +import { ActivityLogs, Customers, Deals, Segments } from '../db/models'; |
| 3 | + |
| 4 | +import { activityLogFactory, customerFactory, dealFactory, segmentFactory } from '../db/factories'; |
| 5 | +import './setup.ts'; |
| 6 | + |
| 7 | +describe('Test activity model', () => { |
| 8 | + afterEach(async () => { |
| 9 | + // Clearing test data |
| 10 | + await ActivityLogs.deleteMany({}); |
| 11 | + await Deals.deleteMany({}); |
| 12 | + await Segments.deleteMany({}); |
| 13 | + await Customers.deleteMany({}); |
| 14 | + }); |
| 15 | + |
| 16 | + test('Activity add activity', async () => { |
| 17 | + const contentId = faker.random.uuid(); |
| 18 | + const contentType = 'customer'; |
| 19 | + const createdBy = faker.random.uuid(); |
| 20 | + const action = 'create'; |
| 21 | + |
| 22 | + const activity = await ActivityLogs.addActivityLog({ |
| 23 | + contentId, |
| 24 | + contentType, |
| 25 | + createdBy, |
| 26 | + action, |
| 27 | + }); |
| 28 | + |
| 29 | + expect(activity).toBeDefined(); |
| 30 | + expect(activity.contentId).toEqual(contentId); |
| 31 | + expect(activity.contentType).toEqual(contentType); |
| 32 | + expect(activity.createdBy).toEqual(createdBy); |
| 33 | + expect(activity.action).toEqual(action); |
| 34 | + }); |
| 35 | + |
| 36 | + test('Activity remove activity', async () => { |
| 37 | + const activity = await activityLogFactory(); |
| 38 | + |
| 39 | + await ActivityLogs.removeActivityLog(activity.contentId); |
| 40 | + |
| 41 | + const count = await ActivityLogs.find({ contentId: activity.contentId }).countDocuments(); |
| 42 | + |
| 43 | + expect(count).toBe(0); |
| 44 | + }); |
| 45 | + |
| 46 | + test('Activity create board item log', async () => { |
| 47 | + const deal = await dealFactory({}); |
| 48 | + |
| 49 | + const activity = await ActivityLogs.createBoardItemLog({ item: deal, contentType: 'deal' }); |
| 50 | + |
| 51 | + expect(activity.contentId).toEqual(deal._id); |
| 52 | + }); |
| 53 | + |
| 54 | + test('Activity create log from widget', async () => { |
| 55 | + const item = await customerFactory({}); |
| 56 | + |
| 57 | + const activity1 = await ActivityLogs.createLogFromWidget('create-customer', item); |
| 58 | + const activity2 = await ActivityLogs.createLogFromWidget('create-company', item); |
| 59 | + |
| 60 | + expect(activity1.contentId).toEqual(item._id); |
| 61 | + expect(activity2.contentId).toEqual(item._id); |
| 62 | + }); |
| 63 | + |
| 64 | + test('Activity create coc log', async () => { |
| 65 | + const item = await customerFactory({ mergedIds: ['1', '2'] }); |
| 66 | + const item2 = await customerFactory({ integrationId: '123', ownerId: undefined }); |
| 67 | + |
| 68 | + const activity1 = await ActivityLogs.createCocLog({ coc: item, contentType: 'customer' }); |
| 69 | + const activity2 = await ActivityLogs.createCocLog({ coc: item2, contentType: 'customer' }); |
| 70 | + |
| 71 | + expect(activity1.contentId).toEqual(item._id); |
| 72 | + expect(activity2.contentId).toEqual(item2._id); |
| 73 | + }); |
| 74 | + |
| 75 | + test('Activity create board item movement log', async () => { |
| 76 | + const item = await dealFactory({}); |
| 77 | + |
| 78 | + const activity1 = await ActivityLogs.createBoardItemMovementLog(item, 'deal', '123', {}); |
| 79 | + |
| 80 | + expect(activity1.contentId).toEqual(item._id); |
| 81 | + }); |
| 82 | + |
| 83 | + test('Activity create board item movement log', async () => { |
| 84 | + const customer = await customerFactory({}); |
| 85 | + const segment1 = await segmentFactory({}); |
| 86 | + const segment2 = await segmentFactory({}); |
| 87 | + |
| 88 | + const foundedActivity = await ActivityLogs.create({ |
| 89 | + contentType: 'customer', |
| 90 | + action: 'segment', |
| 91 | + contentId: customer._id, |
| 92 | + content: { |
| 93 | + id: segment1._id, |
| 94 | + content: segment2, |
| 95 | + }, |
| 96 | + }); |
| 97 | + |
| 98 | + const activity1 = await ActivityLogs.createSegmentLog(segment1, customer, 'customer'); |
| 99 | + const activity2 = await ActivityLogs.createSegmentLog(segment2, customer, 'customer'); |
| 100 | + |
| 101 | + expect(activity1._id).toEqual(foundedActivity._id); |
| 102 | + expect(activity2._id).toEqual(activity2._id); |
| 103 | + }); |
| 104 | +}); |
0 commit comments