-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:(service-worker): added new service-worker compatible SDK export…
… with samples * feat:(service-worker): new service worker compatible SDK added * feat:(service-worker): add build configuration for service worker * feat:(service-worker): updated gitignore file with service worker build artifacts * feat:(service-worker): added unit test for service worker * docs(chrome-extension): added documentation on usage for chrome extensions
- Loading branch information
Showing
43 changed files
with
42,414 additions
and
6,848 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
const identifyRequestPayload = { | ||
userId: '123456', | ||
traits: { | ||
name: 'Name Username', | ||
email: 'name@website.com', | ||
plan: 'Free', | ||
friends: 21, | ||
}, | ||
}; | ||
|
||
const trackRequestPayload = { | ||
userId: '123456', | ||
event: 'Item Viewed', | ||
properties: { | ||
revenue: 19.95, | ||
shippingMethod: 'Premium', | ||
}, | ||
}; | ||
|
||
const pageRequestPayload = { | ||
userId: '12345', | ||
category: 'Food', | ||
name: 'Pizza', | ||
properties: { | ||
url: 'https://dominos.com', | ||
title: 'Pizza', | ||
referrer: 'https://google.com', | ||
}, | ||
}; | ||
|
||
const screenRequestPayload = { | ||
userId: '12345', | ||
category: 'Food', | ||
name: 'Pizza', | ||
properties: { | ||
screenSize: 10, | ||
title: 'Pizza', | ||
referrer: 'https://google.com', | ||
}, | ||
}; | ||
|
||
const groupRequestPayload = { | ||
userId: '12345', | ||
groupId: '1', | ||
traits: { | ||
name: 'Company', | ||
description: 'Google', | ||
}, | ||
}; | ||
|
||
const aliasRequestPayload = { | ||
previousId: 'old_id', | ||
userId: 'new_id', | ||
}; | ||
|
||
const dummyWriteKey = 'dummyWriteKey'; | ||
|
||
const dummyDataplaneHost = 'https://dummy.dataplane.host.com'; | ||
|
||
const dummyInitOptions = { | ||
timeout: false, | ||
flushAt: 1, | ||
flushInterval: 200000, | ||
maxInternalQueueSize: 1, | ||
logLevel: 'off', | ||
enable: true, | ||
}; | ||
|
||
export { | ||
identifyRequestPayload, | ||
trackRequestPayload, | ||
pageRequestPayload, | ||
screenRequestPayload, | ||
groupRequestPayload, | ||
aliasRequestPayload, | ||
dummyWriteKey, | ||
dummyInitOptions, | ||
dummyDataplaneHost, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { setupServer } from 'msw/node'; | ||
import { dummyDataplaneHost } from './fixtures'; | ||
|
||
const server = setupServer({ | ||
url: `${dummyDataplaneHost}/v1/batch`, | ||
response: () => null, | ||
status: 200, | ||
method: 'post', | ||
responseHeaders: { Environment: 'local' }, | ||
}); | ||
|
||
export { server }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { advanceTo } from 'jest-date-mock'; | ||
import { Analytics } from '../../service-worker'; | ||
import { server } from './__mocks__/msw.server'; | ||
import { | ||
aliasRequestPayload, | ||
dummyDataplaneHost, | ||
dummyInitOptions, | ||
dummyWriteKey, | ||
groupRequestPayload, | ||
identifyRequestPayload, | ||
pageRequestPayload, | ||
screenRequestPayload, | ||
trackRequestPayload, | ||
} from './__mocks__/fixtures'; | ||
|
||
jest.mock('uuid', () => ({ v4: () => '123456789' })); | ||
|
||
describe('JS SDK Service Worker', () => { | ||
let rudderAnalyticsClient = null; | ||
let requestBody; | ||
|
||
beforeAll(() => { | ||
advanceTo(new Date(2022, 1, 21, 0, 0, 0)); | ||
server.listen(); | ||
}); | ||
|
||
beforeEach(() => { | ||
rudderAnalyticsClient = new Analytics(dummyWriteKey, dummyDataplaneHost, dummyInitOptions); | ||
server.events.on('request:start', (req) => { | ||
requestBody = req.body; | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
rudderAnalyticsClient = null; | ||
server.resetHandlers(); | ||
server.events.removeAllListeners(); | ||
requestBody = null; | ||
}); | ||
|
||
afterAll(() => { | ||
server.close(); | ||
}); | ||
|
||
it('Should initialise with correct values', () => { | ||
expect(rudderAnalyticsClient.writeKey).toBe(dummyWriteKey); | ||
expect(rudderAnalyticsClient.host).toBe(dummyDataplaneHost); | ||
expect(rudderAnalyticsClient.timeout).toBe(dummyInitOptions.timeout); | ||
expect(rudderAnalyticsClient.flushAt).toBe(dummyInitOptions.flushAt); | ||
expect(rudderAnalyticsClient.flushInterval).toBe(dummyInitOptions.flushInterval); | ||
expect(rudderAnalyticsClient.maxInternalQueueSize).toBe(dummyInitOptions.maxInternalQueueSize); | ||
expect(rudderAnalyticsClient.logLevel).toBe(dummyInitOptions.logLevel); | ||
expect(rudderAnalyticsClient.enable).toBe(dummyInitOptions.enable); | ||
}); | ||
|
||
it('Should record identify', async () => { | ||
rudderAnalyticsClient.identify(identifyRequestPayload); | ||
rudderAnalyticsClient.flush(); | ||
|
||
await new Promise((r) => setTimeout(r, 1)); | ||
|
||
expect(requestBody.batch[0]).toEqual(expect.objectContaining(identifyRequestPayload)); | ||
}); | ||
|
||
it('Should record track', async () => { | ||
rudderAnalyticsClient.track(trackRequestPayload); | ||
rudderAnalyticsClient.flush(); | ||
|
||
await new Promise((r) => setTimeout(r, 1)); | ||
|
||
expect(requestBody.batch[0]).toEqual(expect.objectContaining(trackRequestPayload)); | ||
}); | ||
|
||
it('Should record page', async () => { | ||
rudderAnalyticsClient.page(pageRequestPayload); | ||
rudderAnalyticsClient.flush(); | ||
|
||
await new Promise((r) => setTimeout(r, 1)); | ||
|
||
expect(requestBody.batch[0]).toEqual(expect.objectContaining(pageRequestPayload)); | ||
}); | ||
|
||
it('Should record screen', async () => { | ||
rudderAnalyticsClient.screen(screenRequestPayload); | ||
rudderAnalyticsClient.flush(); | ||
|
||
await new Promise((r) => setTimeout(r, 1)); | ||
|
||
expect(requestBody.batch[0]).toEqual(expect.objectContaining(screenRequestPayload)); | ||
}); | ||
|
||
it('Should record group', async () => { | ||
rudderAnalyticsClient.group(groupRequestPayload); | ||
rudderAnalyticsClient.flush(); | ||
|
||
await new Promise((r) => setTimeout(r, 1)); | ||
|
||
expect(requestBody.batch[0]).toEqual(expect.objectContaining(groupRequestPayload)); | ||
}); | ||
|
||
it('Should record alias', async () => { | ||
rudderAnalyticsClient.alias(aliasRequestPayload); | ||
rudderAnalyticsClient.flush(); | ||
|
||
await new Promise((r) => setTimeout(r, 1)); | ||
|
||
expect(requestBody.batch[0]).toEqual(expect.objectContaining(aliasRequestPayload)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require('isomorphic-fetch'); |
Oops, something went wrong.