|
| 1 | +import assert from 'assert'; |
| 2 | +import { Client, Role } from '../lib'; |
| 3 | +import nock from 'nock'; |
| 4 | + |
| 5 | +describe('visitors', () => { |
| 6 | + const client = new Client({ |
| 7 | + usernameAuth: { username: 'foo', password: 'bar' }, |
| 8 | + }); |
| 9 | + it('retrieves a visitor by id', async () => { |
| 10 | + const id = 'baz'; |
| 11 | + |
| 12 | + nock('https://api.intercom.io').get(`/visitors/${id}`).reply(200, {}); |
| 13 | + |
| 14 | + const response = await client.visitors.find({ id }); |
| 15 | + |
| 16 | + assert.deepStrictEqual({}, response); |
| 17 | + }); |
| 18 | + it('retrieves a visitor by user id', async () => { |
| 19 | + const userId = 'baz'; |
| 20 | + |
| 21 | + nock('https://api.intercom.io') |
| 22 | + .get('/visitors') |
| 23 | + .query({ user_id: userId }) |
| 24 | + .reply(200, {}); |
| 25 | + |
| 26 | + const response = await client.visitors.find({ userId }); |
| 27 | + |
| 28 | + assert.deepStrictEqual({}, response); |
| 29 | + }); |
| 30 | + it('updates a visitor by id', async () => { |
| 31 | + const requestData = { |
| 32 | + user_id: '124', |
| 33 | + name: 'Winston Smith', |
| 34 | + custom_attributes: { |
| 35 | + paid_subscriber: true, |
| 36 | + monthly_spend: 155.5, |
| 37 | + team_mates: 9, |
| 38 | + }, |
| 39 | + }; |
| 40 | + nock('https://api.intercom.io').put('/visitors').reply(200, {}); |
| 41 | + const response = await client.visitors.update({ |
| 42 | + userId: requestData.user_id, |
| 43 | + name: requestData.name, |
| 44 | + customAttributes: requestData.custom_attributes, |
| 45 | + }); |
| 46 | + |
| 47 | + assert.deepStrictEqual({}, response); |
| 48 | + }); |
| 49 | + it('deletes a visitor', async () => { |
| 50 | + const id = 'baz'; |
| 51 | + |
| 52 | + nock('https://api.intercom.io') |
| 53 | + .delete(`/visitors/${id}`) |
| 54 | + .reply(200, {}); |
| 55 | + |
| 56 | + const response = await client.visitors.delete({ |
| 57 | + id, |
| 58 | + }); |
| 59 | + |
| 60 | + assert.deepStrictEqual({}, response); |
| 61 | + }); |
| 62 | + it('converts a visitor into contact', async () => { |
| 63 | + const requestData = { |
| 64 | + visitor: { id: 'baz' }, |
| 65 | + user: { user_id: 'bez' }, |
| 66 | + type: Role.USER, |
| 67 | + }; |
| 68 | + |
| 69 | + nock('https://api.intercom.io') |
| 70 | + .post('/visitors/convert', requestData) |
| 71 | + .reply(200, {}); |
| 72 | + |
| 73 | + const response = await client.visitors.mergeToContact({ |
| 74 | + visitor: { |
| 75 | + id: requestData.visitor.id, |
| 76 | + }, |
| 77 | + user: { |
| 78 | + userId: requestData.user.user_id, |
| 79 | + }, |
| 80 | + type: requestData.type, |
| 81 | + }); |
| 82 | + |
| 83 | + assert.deepStrictEqual({}, response); |
| 84 | + }); |
| 85 | +}); |
0 commit comments