Skip to content

Commit

Permalink
Restoring some tests after refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelGHSeg committed Sep 26, 2023
1 parent 3e4f72a commit 187fb28
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ describe('Ability for users to exit without losing events', () => {
expect(elapsedTime).toBeLessThan(100)
const calls = _helpers.getFetchCalls()
expect(calls.length).toBe(1)
//expect(calls[0].body.batch.length).toBe(2)
expect(JSON.parse(calls[0].body).batch.length).toBe(2)
})

test('should wait to flush if close is called and an event has not made it to the segment.io plugin yet', async () => {
Expand Down Expand Up @@ -238,7 +238,7 @@ describe('Ability for users to exit without losing events', () => {
expect(elapsedTime).toBeLessThan(TRACK_DELAY * 2)
const calls = _helpers.getFetchCalls()
expect(calls.length).toBe(1)
//expect(calls[0].body.batch.length).toBe(2)
expect(JSON.parse(calls[0].body).batch.length).toBe(2)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { HTTPClientRequest } from '../../../lib/http-client'
*/
export const httpClientOptionsBodyMatcher = {
messageId: expect.stringMatching(/^node-next-\d*-\w*-\w*-\w*-\w*-\w*/),
timestamp: expect.any(Date),
timestamp: expect.stringMatching(
/^20\d{2}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d+Z/
),
_metadata: expect.any(Object),
context: {
library: {
Expand All @@ -29,6 +31,5 @@ export function assertHTTPRequestOptions(
"User-Agent": "analytics-node-next/latest",
}
`)
//expect(body).toHaveLength(contexts.length)
expect(body.length).toBeGreaterThan(contexts.length) // Just to make errors go away, please suggest something
expect(JSON.parse(body).batch).toHaveLength(contexts.length)
}
30 changes: 15 additions & 15 deletions packages/node/src/__tests__/typedef-tests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-var-requires */
//import axios from 'axios'
import axios from 'axios'
import {
Analytics,
Context,
Expand All @@ -9,7 +9,7 @@ import {
HTTPClient,
FetchHTTPClient,
HTTPFetchFn,
//HTTPClientRequest,
HTTPClientRequest,
} from '../'

/**
Expand Down Expand Up @@ -104,17 +104,17 @@ export default {
new Analytics({ writeKey: 'foo', httpClient: {} as BadFetch })
},

// 'httpClient setting should be compatible with axios': () => {
// new (class implements HTTPClient {
// async makeRequest(options: HTTPClientRequest) {
// return axios({
// url: options.url,
// method: options.method,
// data: options.body,
// headers: options.headers,
// timeout: options.httpRequestTimeout,
// })
// }
// })()
// },
'httpClient setting should be compatible with axios': () => {
new (class implements HTTPClient {
async makeRequest(options: HTTPClientRequest) {
return axios({
url: options.url,
method: options.method,
data: options.body,
headers: options.headers,
timeout: options.httpRequestTimeout,
})
}
})()
},
}
155 changes: 78 additions & 77 deletions packages/node/src/plugins/segmentio/__tests__/methods.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Context } from '../../../app/context'
import { Emitter } from '@segment/analytics-core'
import {
assertHTTPRequestOptions,
//httpClientOptionsBodyMatcher,
httpClientOptionsBodyMatcher,
} from '../../../__tests__/test-helpers/assert-shape'
import { TestFetchClient } from '../../../__tests__/test-helpers/create-test-analytics'

Expand Down Expand Up @@ -52,16 +52,16 @@ test('alias', async () => {
expect(fetcher).toHaveBeenCalledTimes(1)
validateFetcherInputs(context)

// const [request] = fetcher.mock.lastCall
// const data = request.data

// expect(data.batch).toHaveLength(1)
// expect(data.batch[0]).toEqual({
// ...httpClientOptionsBodyMatcher,
// type: 'alias',
// previousId: 'from',
// userId: 'to',
// })
const [request] = fetcher.mock.lastCall
const data = JSON.parse(request.body)

expect(data.batch).toHaveLength(1)
expect(data.batch[0]).toEqual({
...httpClientOptionsBodyMatcher,
type: 'alias',
previousId: 'from',
userId: 'to',
})
})

test('group', async () => {
Expand All @@ -82,19 +82,19 @@ test('group', async () => {
expect(fetcher).toHaveBeenCalledTimes(1)
validateFetcherInputs(context)

// const [request] = fetcher.mock.lastCall
// const data = request.data

// expect(data.batch).toHaveLength(1)
// expect(data.batch[0]).toEqual({
// ...httpClientOptionsBodyMatcher,
// traits: {
// name: 'libraries',
// },
// type: 'group',
// groupId: 'foo-group-id',
// userId: 'foo-user-id',
// })
const [request] = fetcher.mock.lastCall
const data = JSON.parse(request.body)

expect(data.batch).toHaveLength(1)
expect(data.batch[0]).toEqual({
...httpClientOptionsBodyMatcher,
traits: {
name: 'libraries',
},
type: 'group',
groupId: 'foo-group-id',
userId: 'foo-user-id',
})
})

test('identify', async () => {
Expand All @@ -111,17 +111,18 @@ test('identify', async () => {
expect(fetcher).toHaveBeenCalledTimes(1)
validateFetcherInputs(context)

// const [request] = fetcher.mock.lastCall
// const data = request.data
// expect(data.batch).toHaveLength(1)
// expect(data.batch[0]).toEqual({
// ...httpClientOptionsBodyMatcher,
// traits: {
// name: 'Chris Radek',
// },
// type: 'identify',
// userId: 'foo-user-id',
// })
const [request] = fetcher.mock.lastCall
const data = JSON.parse(request.body)

expect(data.batch).toHaveLength(1)
expect(data.batch[0]).toEqual({
...httpClientOptionsBodyMatcher,
traits: {
name: 'Chris Radek',
},
type: 'identify',
userId: 'foo-user-id',
})
})

test('page', async () => {
Expand All @@ -141,21 +142,21 @@ test('page', async () => {
expect(fetcher).toHaveBeenCalledTimes(1)
validateFetcherInputs(context)

// const [request] = fetcher.mock.lastCall
// const data = request.data

// expect(data.batch).toHaveLength(1)
// expect(data.batch[0]).toEqual({
// ...httpClientOptionsBodyMatcher,
// type: 'page',
// userId: 'foo-user-id',
// name: 'Home',
// category: 'Category',
// properties: {
// category: 'Category',
// url: 'http://localhost',
// },
// })
const [request] = fetcher.mock.lastCall
const data = JSON.parse(request.body)

expect(data.batch).toHaveLength(1)
expect(data.batch[0]).toEqual({
...httpClientOptionsBodyMatcher,
type: 'page',
userId: 'foo-user-id',
name: 'Home',
category: 'Category',
properties: {
category: 'Category',
url: 'http://localhost',
},
})
})

test('screen', async () => {
Expand All @@ -175,20 +176,20 @@ test('screen', async () => {
expect(fetcher).toHaveBeenCalledTimes(1)
validateFetcherInputs(context)

// const [request] = fetcher.mock.lastCall
// const data = request.data

// expect(data.batch).toHaveLength(1)
// expect(data.batch[0]).toEqual({
// ...httpClientOptionsBodyMatcher,
// type: 'screen',
// userId: 'foo-user-id',
// name: 'Home',
// category: 'Category',
// properties: {
// variation: 'local',
// },
// })
const [request] = fetcher.mock.lastCall
const data = JSON.parse(request.body)

expect(data.batch).toHaveLength(1)
expect(data.batch[0]).toEqual({
...httpClientOptionsBodyMatcher,
type: 'screen',
userId: 'foo-user-id',
name: 'Home',
category: 'Category',
properties: {
variation: 'local',
},
})
})

test('track', async () => {
Expand All @@ -207,17 +208,17 @@ test('track', async () => {
expect(fetcher).toHaveBeenCalledTimes(1)
validateFetcherInputs(context)

// const [request] = fetcher.mock.lastCall
// const data = request.data

// expect(data.batch).toHaveLength(1)
// expect(data.batch[0]).toEqual({
// ...httpClientOptionsBodyMatcher,
// type: 'track',
// event: 'test event',
// userId: 'foo-user-id',
// properties: {
// foo: 'bar',
// },
// })
const [request] = fetcher.mock.lastCall
const data = JSON.parse(request.body)

expect(data.batch).toHaveLength(1)
expect(data.batch[0]).toEqual({
...httpClientOptionsBodyMatcher,
type: 'track',
event: 'test event',
userId: 'foo-user-id',
properties: {
foo: 'bar',
},
})
})

0 comments on commit 187fb28

Please sign in to comment.