-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reorg ts lib as classes, webpack bundle updates, http-client-wrapper (#…
…986) * reorg ts lib as classes, webpack bundle updates, http-client-wrapper * remove console.logs * add import change to mocks * add logic to catch if custom httpClient option utilized in non-lite bundle, also removed some unused bits * update uuid import and correct usage for clientsessionId, updated specs * use concurrently and break up build types scripts * 5.1 version update (#984) * 5.1 version update * remove comment * update Scheduler to webpack 5, node 18 (#990) * fix for jumbled sorting of exp list for lower and upper case exp names (#991) * remove auto-merge workflow (#992) * add smarter response types * add smarter response types * fix error handling on bad requests or end of retries * remove async await from most ApiService calls, can just pass Promise straight through no problem * get api version from package json and inject via webpack * add api_version to jest config * clean up lib tester code, pull in correct versions * fixed payload type error (#1007) (#1008) Co-authored-by: Yagnik Hingrajiya <[email protected]> * try using workflow branch input as commit target in release flow (#1009) * remove no-assignment logic --------- Co-authored-by: Ben Blanchard <[email protected]> Co-authored-by: Pratik Prajapati <[email protected]> Co-authored-by: Yagnik Hingrajiya <[email protected]>
- Loading branch information
1 parent
ab68afe
commit 00b3114
Showing
45 changed files
with
3,411 additions
and
2,139 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,3 +32,4 @@ java/.project | |
java/.settings | ||
java/target | ||
|
||
coverage/ |
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,285 @@ | ||
import { CaliperEnvelope } from 'upgrade_types'; | ||
import ApiService from './ApiService'; | ||
import { UpGradeClientInterfaces } from './../types/Interfaces'; | ||
import { UpGradeClientRequests } from './../types/requests'; | ||
|
||
const MockDataService = { | ||
findExperimentAssignmentBySiteAndTarget: jest.fn(), | ||
rotateAssignmentList: jest.fn(), | ||
}; | ||
|
||
const mockHttpClient = { | ||
doGet: jest.fn(), | ||
doPost: jest.fn(), | ||
doPatch: jest.fn(), | ||
}; | ||
|
||
const defaultConfig: UpGradeClientInterfaces.IConfig = { | ||
hostURL: 'test.com', | ||
userId: 'abc123', | ||
context: 'context', | ||
apiVersion: 'v5', | ||
clientSessionId: 'testClientSessionId', | ||
token: 'testToken', | ||
httpClient: mockHttpClient, | ||
}; | ||
|
||
describe('ApiService', () => { | ||
let apiService: ApiService; | ||
|
||
beforeEach(() => { | ||
apiService = new ApiService(defaultConfig, MockDataService as any); | ||
}); | ||
|
||
// these tests internally call through private methods sendRequest and createOptions... | ||
// the assertion will be that the request body will get mapped to the correct params | ||
// for the http client provided, which is itself mocked and can be spied | ||
|
||
describe('#init', () => { | ||
const expectedUrl = `${defaultConfig.hostURL}/api/${defaultConfig.apiVersion}/init`; | ||
const expectedOptions = { | ||
headers: { | ||
'Client-source': 'browser', | ||
'Content-Type': 'application/json', | ||
'Session-Id': 'testClientSessionId', | ||
URL: expectedUrl, | ||
Authorization: 'Bearer testToken', | ||
}, | ||
withCredentials: false, | ||
}; | ||
|
||
it('should call sendRequest with just id', async () => { | ||
const requestBody: UpGradeClientInterfaces.IExperimentUser = { | ||
id: defaultConfig.userId, | ||
}; | ||
|
||
await apiService.init(); | ||
|
||
expect(mockHttpClient.doPost).toHaveBeenCalledWith(expectedUrl, requestBody, expectedOptions); | ||
}); | ||
|
||
it('should call sendRequest with id and group', async () => { | ||
const mockGroup: UpGradeClientInterfaces.IExperimentUserGroup = { | ||
school: ['testGroupSchool'], | ||
}; | ||
const requestBody: UpGradeClientInterfaces.IExperimentUser = { | ||
id: defaultConfig.userId, | ||
group: mockGroup, | ||
}; | ||
|
||
await apiService.init(mockGroup); | ||
|
||
expect(mockHttpClient.doPost).toHaveBeenCalledWith(expectedUrl, requestBody, expectedOptions); | ||
}); | ||
|
||
it('should call sendRequest with id and workingGroup', async () => { | ||
const mockWorkingGroup: UpGradeClientInterfaces.IExperimentUserWorkingGroup = { | ||
school: 'testWorkingGroupSchool', | ||
}; | ||
const requestBody: UpGradeClientInterfaces.IExperimentUser = { | ||
id: defaultConfig.userId, | ||
workingGroup: mockWorkingGroup, | ||
}; | ||
|
||
await apiService.init(undefined, mockWorkingGroup); | ||
|
||
expect(mockHttpClient.doPost).toHaveBeenCalledWith(expectedUrl, requestBody, expectedOptions); | ||
}); | ||
|
||
it('should call sendRequest with id, group, and workingGroup', async () => { | ||
const mockGroup: UpGradeClientInterfaces.IExperimentUserGroup = { | ||
school: ['testGroupSchool'], | ||
}; | ||
const mockWorkingGroup: UpGradeClientInterfaces.IExperimentUserWorkingGroup = { | ||
school: 'testWorkingGroupSchool', | ||
}; | ||
const requestBody: UpGradeClientInterfaces.IExperimentUser = { | ||
id: defaultConfig.userId, | ||
group: mockGroup, | ||
workingGroup: mockWorkingGroup, | ||
}; | ||
|
||
await apiService.init(mockGroup, mockWorkingGroup); | ||
|
||
expect(mockHttpClient.doPost).toHaveBeenCalledWith(expectedUrl, requestBody, expectedOptions); | ||
}); | ||
}); | ||
|
||
describe('#setGroupMembership', () => { | ||
const expectedUrl = `${defaultConfig.hostURL}/api/${defaultConfig.apiVersion}/groupmembership`; | ||
const expectedOptions = { | ||
headers: { | ||
'Client-source': 'browser', | ||
'Content-Type': 'application/json', | ||
'Session-Id': 'testClientSessionId', | ||
URL: expectedUrl, | ||
Authorization: 'Bearer testToken', | ||
}, | ||
withCredentials: false, | ||
}; | ||
|
||
it('should call sendRequest with id and group', async () => { | ||
const mockGroup: UpGradeClientInterfaces.IExperimentUserGroup = { | ||
school: ['testGroupSchool'], | ||
}; | ||
const requestBody: UpGradeClientInterfaces.IExperimentUser = { | ||
id: defaultConfig.userId, | ||
group: mockGroup, | ||
}; | ||
|
||
await apiService.setGroupMembership(mockGroup); | ||
|
||
expect(mockHttpClient.doPatch).toHaveBeenCalledWith(expectedUrl, requestBody, expectedOptions); | ||
}); | ||
}); | ||
|
||
describe('#setWorkingGroup', () => { | ||
//mimic setGroupMembership tests | ||
const expectedUrl = `${defaultConfig.hostURL}/api/${defaultConfig.apiVersion}/workinggroup`; | ||
const expectedOptions = { | ||
headers: { | ||
'Client-source': 'browser', | ||
'Content-Type': 'application/json', | ||
'Session-Id': 'testClientSessionId', | ||
URL: expectedUrl, | ||
Authorization: 'Bearer testToken', | ||
}, | ||
withCredentials: false, | ||
}; | ||
|
||
it('should call sendRequest with id and workingGroup', async () => { | ||
const mockWorkingGroup: UpGradeClientInterfaces.IExperimentUserWorkingGroup = { | ||
school: 'testWorkingGroupSchool', | ||
}; | ||
const requestBody: UpGradeClientInterfaces.IExperimentUser = { | ||
id: defaultConfig.userId, | ||
workingGroup: mockWorkingGroup, | ||
}; | ||
|
||
await apiService.setWorkingGroup(mockWorkingGroup); | ||
|
||
expect(mockHttpClient.doPatch).toHaveBeenCalledWith(expectedUrl, requestBody, expectedOptions); | ||
}); | ||
}); | ||
|
||
describe('#setAltUserIds', () => { | ||
const expectedUrl = `${defaultConfig.hostURL}/api/${defaultConfig.apiVersion}/useraliases`; | ||
const expectedOptions = { | ||
headers: { | ||
'Client-source': 'browser', | ||
'Content-Type': 'application/json', | ||
'Session-Id': 'testClientSessionId', | ||
URL: expectedUrl, | ||
Authorization: 'Bearer testToken', | ||
}, | ||
withCredentials: false, | ||
}; | ||
|
||
it('should call sendRequest with id and altUserIds', async () => { | ||
const mockAliases = ['asdf', '1234']; | ||
const requestBody: UpGradeClientRequests.ISetAltIdsRequestBody = { | ||
userId: defaultConfig.userId, | ||
aliases: mockAliases, | ||
}; | ||
|
||
await apiService.setAltUserIds(mockAliases); | ||
|
||
expect(mockHttpClient.doPatch).toHaveBeenCalledWith(expectedUrl, requestBody, expectedOptions); | ||
}); | ||
}); | ||
|
||
describe('#getAllExperimentConditions', () => { | ||
const expectedUrl = `${defaultConfig.hostURL}/api/${defaultConfig.apiVersion}/assign`; | ||
const expectedOptions = { | ||
headers: { | ||
'Client-source': 'browser', | ||
'Content-Type': 'application/json', | ||
'Session-Id': 'testClientSessionId', | ||
URL: expectedUrl, | ||
Authorization: 'Bearer testToken', | ||
}, | ||
withCredentials: false, | ||
}; | ||
|
||
it('should call sendRequest with id and context', async () => { | ||
const requestBody: UpGradeClientRequests.IGetAllExperimentConditionsRequestBody = { | ||
userId: defaultConfig.userId, | ||
context: defaultConfig.context, | ||
}; | ||
|
||
await apiService.getAllExperimentConditions(); | ||
|
||
expect(mockHttpClient.doPost).toHaveBeenCalledWith(expectedUrl, requestBody, expectedOptions); | ||
}); | ||
}); | ||
|
||
describe('#log', () => { | ||
const expectedUrl = `${defaultConfig.hostURL}/api/${defaultConfig.apiVersion}/log`; | ||
const expectedOptions = { | ||
headers: { | ||
'Client-source': 'browser', | ||
'Content-Type': 'application/json', | ||
'Session-Id': 'testClientSessionId', | ||
URL: expectedUrl, | ||
Authorization: 'Bearer testToken', | ||
}, | ||
withCredentials: false, | ||
}; | ||
|
||
it('should call sendRequest with userId and logDataInput value', async () => { | ||
const mockLogData = [ | ||
{ | ||
timestamp: '1234', | ||
metrics: { | ||
attributes: { | ||
testAttribute: 'testValue', | ||
}, | ||
groupedMetrics: [ | ||
{ | ||
groupClass: 'workspaces', | ||
groupKey: 'abc', | ||
groupUniquifier: 'abc123', | ||
attributes: [] as any, | ||
}, | ||
], | ||
}, | ||
}, | ||
]; | ||
const mockLogDataInput: UpGradeClientRequests.ILogRequestBody = { | ||
userId: defaultConfig.userId, | ||
value: mockLogData, | ||
}; | ||
|
||
await apiService.log(mockLogData); | ||
|
||
expect(mockHttpClient.doPost).toHaveBeenCalledWith(expectedUrl, mockLogDataInput, expectedOptions); | ||
}); | ||
}); | ||
|
||
describe('#logCaliper', () => { | ||
const expectedUrl = `${defaultConfig.hostURL}/api/${defaultConfig.apiVersion}/log/caliper`; | ||
const expectedOptions = { | ||
headers: { | ||
'Client-source': 'browser', | ||
'Content-Type': 'application/json', | ||
'Session-Id': 'testClientSessionId', | ||
URL: expectedUrl, | ||
Authorization: 'Bearer testToken', | ||
}, | ||
withCredentials: false, | ||
}; | ||
|
||
it('should call sendRequest with caliper envelope value', async () => { | ||
const mockLogData: CaliperEnvelope = { | ||
sensor: 'test', | ||
sendTime: '12345678', | ||
dataVersion: '1', | ||
data: [], | ||
}; | ||
|
||
await apiService.logCaliper(mockLogData); | ||
|
||
expect(mockHttpClient.doPost).toHaveBeenCalledWith(expectedUrl, mockLogData, expectedOptions); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.