Skip to content
This repository has been archived by the owner on Mar 16, 2023. It is now read-only.

Commit

Permalink
fix(sdk): fix and update tests for ApiManager
Browse files Browse the repository at this point in the history
  • Loading branch information
LeilaWang committed Feb 24, 2020
1 parent 828f428 commit d00a579
Showing 1 changed file with 61 additions and 10 deletions.
71 changes: 61 additions & 10 deletions packages/extension/src/client/Aztec/__tests__/ApiManager.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,14 @@ describe('ApiManager.refreshSession', () => {
const validateContractConfigsSpy = jest.spyOn(ApiPermissionService, 'validateContractConfigs');

const initWeb3ServiceSpy = jest.spyOn(Web3Service, 'init')
.mockImplementation(() => {
Web3Service.account = {
address: 'account_address',
};
Web3Service.networkId = 'network_id';
});

const registerContractsSpy = jest.spyOn(Web3Service, 'registerContractsConfig')
.mockImplementation(jest.fn());

const aztecAccount = {
Expand All @@ -377,7 +385,14 @@ describe('ApiManager.refreshSession', () => {
}));

const defaultApis = {
run: jest.fn(),
web3: {
account: {
address: 'account_address',
},
network: {
id: 'network_id',
},
},
};
const fullApis = {
run: jest.fn(),
Expand All @@ -389,7 +404,7 @@ describe('ApiManager.refreshSession', () => {
const options = {
apiKey,
contractAddresses,
providerUrl: '',
providerUrl: 'provider_url',
};
let mockSetApis;
let resolveSessionSpy;
Expand All @@ -398,6 +413,7 @@ describe('ApiManager.refreshSession', () => {
openConnectionSpy.mockClear();
validateContractConfigsSpy.mockClear();
initWeb3ServiceSpy.mockClear();
registerContractsSpy.mockClear();
ensurePermissionSpy.mockClear();
generateApisSpy.mockClear();

Expand All @@ -413,8 +429,9 @@ describe('ApiManager.refreshSession', () => {
});

afterAll(() => {
openConnectionSpy.mockRestore();
initWeb3ServiceSpy.mockRestore();
registerContractsSpy.mockRestore();
openConnectionSpy.mockRestore();
ensurePermissionSpy.mockRestore();
generateApisSpy.mockRestore();
});
Expand All @@ -425,16 +442,24 @@ describe('ApiManager.refreshSession', () => {
expect(openConnectionSpy).toHaveBeenCalledTimes(1);
expect(openConnectionSpy).toHaveBeenCalledWith(options);

const {
providerUrl,
} = options;
const {
error,
...networkConfig
} = generateNetworkConfig(options);

expect(initWeb3ServiceSpy).toHaveBeenCalledTimes(1);
expect(initWeb3ServiceSpy).toHaveBeenCalledWith({
providerUrl,
});

expect(validateContractConfigsSpy).toHaveBeenCalledTimes(1);
expect(validateContractConfigsSpy).toHaveBeenCalledWith(networkConfig);

expect(initWeb3ServiceSpy).toHaveBeenCalledTimes(1);
expect(initWeb3ServiceSpy).toHaveBeenCalledWith(networkConfig);
expect(registerContractsSpy).toHaveBeenCalledTimes(1);
expect(registerContractsSpy).toHaveBeenCalledWith(networkConfig.contractsConfig);

expect(ensurePermissionSpy).toHaveBeenCalledTimes(1);

Expand All @@ -457,8 +482,9 @@ describe('ApiManager.refreshSession', () => {

await manager.refreshSession(options);

expect(initWeb3ServiceSpy).toHaveBeenCalledTimes(1);
expect(openConnectionSpy).toHaveBeenCalledTimes(1);
expect(initWeb3ServiceSpy).toHaveBeenCalledTimes(0);
expect(registerContractsSpy).toHaveBeenCalledTimes(0);
expect(ensurePermissionSpy).toHaveBeenCalledTimes(0);
expect(resolveSessionSpy).toHaveBeenCalledTimes(1);
expect(resolveSessionSpy).toHaveBeenCalledWith(options, {
Expand All @@ -472,8 +498,10 @@ describe('ApiManager.refreshSession', () => {
message: 'an error occurred',
};
const taskSpies = [
validateContractConfigsSpy,
initWeb3ServiceSpy,
openConnectionSpy,
validateContractConfigsSpy,
registerContractsSpy,
ensurePermissionSpy,
];

Expand Down Expand Up @@ -525,9 +553,10 @@ describe('ApiManager.refreshSession', () => {
apiKey: `${options.apiKey}-2`,
};
const taskSpies = [
initWeb3ServiceSpy,
openConnectionSpy,
validateContractConfigsSpy,
initWeb3ServiceSpy,
registerContractsSpy,
ensurePermissionSpy,
];

Expand Down Expand Up @@ -605,9 +634,10 @@ describe('ApiManager.refreshSession', () => {
two: 2,
};
const taskSpies = [
initWeb3ServiceSpy,
openConnectionSpy,
validateContractConfigsSpy,
initWeb3ServiceSpy,
registerContractsSpy,
ensurePermissionSpy,
];

Expand Down Expand Up @@ -686,9 +716,10 @@ describe('ApiManager.refreshSession', () => {
.mockImplementation(jest.fn());

const taskSpies = [
initWeb3ServiceSpy,
openConnectionSpy,
validateContractConfigsSpy,
initWeb3ServiceSpy,
registerContractsSpy,
ensurePermissionSpy,
];

Expand Down Expand Up @@ -734,4 +765,24 @@ describe('ApiManager.refreshSession', () => {
refreshSessionSpy.mockRestore();
});
});

it('abort if previous account or netowrk is null and autoRefreshOnProfileChange is false', async () => {
manager.autoRefreshOnProfileChange = false;

initWeb3ServiceSpy.mockImplementationOnce(() => {
Web3Service.account = {
address: '',
};
});

await manager.refreshSession(options);

expect(initWeb3ServiceSpy).toHaveBeenCalledTimes(1);
expect(validateContractConfigsSpy).toHaveBeenCalledTimes(0);
expect(registerContractsSpy).toHaveBeenCalledTimes(0);
expect(ensurePermissionSpy).toHaveBeenCalledTimes(0);
expect(resolveSessionSpy).toHaveBeenCalledTimes(0);

expect(mockSetApis).toHaveBeenLastCalledWith(defaultApis);
});
});

0 comments on commit d00a579

Please sign in to comment.