UNPKG

2.51 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', done => {
58 const outputStream = streamtest.v2.toText((error, data) => {
59 expect(error).toBeFalsy()
60 expect(data).toMatchSnapshot()
61
62 done()
63 })
64 customerGroupsExport.run(outputStream)
65 })
66
67 test('should return error', done => {
68 payload.statusCode = '404'
69 const outputStream = streamtest.v2.toText((error, data) => {
70 expect(error).toBeTruthy()
71 expect(data).toBeFalsy()
72 done()
73 })
74 customerGroupsExport.run(outputStream)
75 })
76 })
77
78 describe('::buildRequest', () => {
79 test('should build request', () => {
80 expect(
81 CustomerGroupsExporter.buildRequest('test-project-key')
82 ).toMatchSnapshot()
83 })
84 })
85
86 describe('::buildUri', () => {
87 test('should build default uri', () => {
88 expect(
89 CustomerGroupsExporter.buildUri('test-project-key')
90 ).toMatchSnapshot()
91 })
92 test('should build where/predicate uri', () => {
93 expect(
94 CustomerGroupsExporter.buildUri('test-project-key', 'key=copperKey')
95 ).toMatchSnapshot()
96 })
97 })
98})