UNPKG

2.62 kBJavaScriptView Raw
1import streamtest from 'streamtest'
2import silentLogger from '../src/utils/silent-logger'
3import CustomerGroupsExporter from '../src/main'
4
5describe('CustomerGroupsExporter', () => {
6 const logger = {
7 ...silentLogger,
8 }
9
10 let customerGroupsExport
11 beforeEach(() => {
12 customerGroupsExport = new CustomerGroupsExporter(
13 {
14 apiConfig: {
15 projectKey: 'test-project-key',
16 },
17 },
18 logger
19 )
20 })
21
22 describe('::constructor', () => {
23 test('should be a function', () => {
24 expect(typeof CustomerGroupsExporter).toBe('function')
25 })
26
27 test('should set default properties', () => {
28 expect(customerGroupsExport.apiConfig).toEqual({
29 projectKey: 'test-project-key',
30 })
31 expect(customerGroupsExport.logger).toEqual(logger)
32 })
33
34 test('should throw error if no `apiConfig` in `options` parameter', () => {
35 expect(
36 () => new CustomerGroupsExporter({ foo: 'bar' })
37 ).toThrowErrorMatchingSnapshot()
38 })
39 })
40
41 describe('::run', () => {
42 let payload
43 beforeEach(() => {
44 payload = {
45 statusCode: 200,
46 body: {
47 results: [{ foo1: 'bar1', key: 'copperKey' }, { foo2: 'bar2' }],
48 },
49 }
50 customerGroupsExport.client.process = jest.fn(
51 async (request, callback) => {
52 await callback(payload)
53 }
54 )
55 })
56
57 test('should write to outputStream', () => {
58 return new Promise(done => {
59 const outputStream = streamtest.v2.toText((error, data) => {
60 expect(error).toBeFalsy()
61 expect(data).toMatchSnapshot()
62
63 done()
64 })
65 customerGroupsExport.run(outputStream)
66 })
67 })
68
69 test('should return error', () => {
70 return new Promise(done => {
71 payload.statusCode = '404'
72 const outputStream = streamtest.v2.toText((error, data) => {
73 expect(error).toBeTruthy()
74 expect(data).toBeFalsy()
75 done()
76 })
77 customerGroupsExport.run(outputStream)
78 })
79 })
80 })
81
82 describe('::buildRequest', () => {
83 test('should build request', () => {
84 expect(
85 CustomerGroupsExporter.buildRequest('test-project-key')
86 ).toMatchSnapshot()
87 })
88 })
89
90 describe('::buildUri', () => {
91 test('should build default uri', () => {
92 expect(
93 CustomerGroupsExporter.buildUri('test-project-key')
94 ).toMatchSnapshot()
95 })
96 test('should build where/predicate uri', () => {
97 expect(
98 CustomerGroupsExporter.buildUri('test-project-key', 'key=copperKey')
99 ).toMatchSnapshot()
100 })
101 })
102})