Skip to content

Commit

Permalink
test: use toHaveBeenCalledOnce (#327)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpeterparker authored Nov 20, 2024
1 parent fce2dae commit b49f541
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 49 deletions.
8 changes: 4 additions & 4 deletions src/agent/custom-http-agent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('CustomHttpAgent', () => {
await CustomHttpAgent.create(agentOptions);

// eslint-disable-next-line @typescript-eslint/unbound-method
expect(httpAgent.HttpAgent.create).toHaveBeenCalledTimes(1);
expect(httpAgent.HttpAgent.create).toHaveBeenCalledOnce();
// eslint-disable-next-line @typescript-eslint/unbound-method
expect(httpAgent.HttpAgent.create).toHaveBeenCalledWith(agentOptions);
});
Expand Down Expand Up @@ -143,7 +143,7 @@ describe('CustomHttpAgent', () => {
it('should call agent on request', async () => {
await agent.request(mockRequestPayload);

expect(spyCall).toHaveBeenCalledTimes(1);
expect(spyCall).toHaveBeenCalledOnce();
expect(spyCall).toHaveBeenCalledWith(mockCanisterId, {
arg: base64ToUint8Array(mockRequestPayload.arg),
effectiveCanisterId: mockCanisterId,
Expand Down Expand Up @@ -308,7 +308,7 @@ describe('CustomHttpAgent', () => {
it('should call agent on request', async () => {
await agent.request(mockRequestPayload);

expect(spyCall).toHaveBeenCalledTimes(1);
expect(spyCall).toHaveBeenCalledOnce();
expect(spyCall).toHaveBeenCalledWith(mockCanisterId, {
arg: base64ToUint8Array(mockRequestPayload.arg),
effectiveCanisterId: mockCanisterId,
Expand All @@ -326,7 +326,7 @@ describe('CustomHttpAgent', () => {
it('should poll for response when status is 202 and no certificate is returned by v3 call', async () => {
await agent.request(mockRequestPayload);

expect(spyPollForResponse).toHaveBeenCalledTimes(1);
expect(spyPollForResponse).toHaveBeenCalledOnce();
});
});

Expand Down
4 changes: 2 additions & 2 deletions src/api/agent.api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('AgentApi', () => {
await agentApi.getAgentTest(signerOptions);

// eslint-disable-next-line @typescript-eslint/unbound-method
expect(CustomHttpAgent.create).toHaveBeenCalledTimes(1);
expect(CustomHttpAgent.create).toHaveBeenCalledOnce();
});

it('should create and cache a new agent for a different identity', async () => {
Expand All @@ -82,7 +82,7 @@ describe('AgentApi', () => {
await agentApi.getAgentTest(signerOptions);

// eslint-disable-next-line @typescript-eslint/unbound-method
expect(CustomHttpAgent.create).toHaveBeenCalledTimes(1);
expect(CustomHttpAgent.create).toHaveBeenCalledOnce();
});
});

Expand Down
4 changes: 2 additions & 2 deletions src/api/icrc21-canister.api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,11 @@ describe('icrc-21.canister.api', () => {

// Ensure that the `CustomHttpAgent.create` is only called once
// eslint-disable-next-line @typescript-eslint/unbound-method
expect(CustomHttpAgent.create).toHaveBeenCalledTimes(1);
expect(CustomHttpAgent.create).toHaveBeenCalledOnce();

// TODO: spyOn nor function does work with vitest and Actor.createActor. Not against a better idea than disabling eslint for next line.
// eslint-disable-next-line @typescript-eslint/unbound-method
expect(Actor.createActor).toHaveBeenCalledTimes(1);
expect(Actor.createActor).toHaveBeenCalledOnce();
});

it('should create a new actor when canisterId changes', async () => {
Expand Down
6 changes: 3 additions & 3 deletions src/handlers/relying-party.handlers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ describe('Relying Party handlers', () => {
it('should bring the popup in front with focus', () => {
requestPermissions({id: testId, popup, origin: testOrigin, params});

expect(focusMock).toHaveBeenCalledTimes(1);
expect(focusMock).toHaveBeenCalledOnce();
});
});

Expand All @@ -247,7 +247,7 @@ describe('Relying Party handlers', () => {
it('should bring the popup in front with focus', () => {
requestAccounts({id: testId, popup, origin: testOrigin});

expect(focusMock).toHaveBeenCalledTimes(1);
expect(focusMock).toHaveBeenCalledOnce();
});
});

Expand All @@ -269,7 +269,7 @@ describe('Relying Party handlers', () => {
it('should bring the popup in front with focus', () => {
requestCallCanister({id: testId, popup, origin: testOrigin, params: mockCallCanisterParams});

expect(focusMock).toHaveBeenCalledTimes(1);
expect(focusMock).toHaveBeenCalledOnce();
});
});
});
52 changes: 26 additions & 26 deletions src/relying-party.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ describe('Relying Party', () => {
'relyingPartyWindow',
expectedOptions
);
expect(window.open).toHaveBeenCalledTimes(1);
expect(window.open).toHaveBeenCalledOnce();

expect(addEventListenerSpy).toHaveBeenCalledWith('message', expect.any(Function));
expect(removeEventListenerSpy).toHaveBeenCalledWith('message', expect.any(Function));
Expand All @@ -182,7 +182,7 @@ describe('Relying Party', () => {

const promise = RelyingParty.connect(mockParameters);

expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledOnce();

expect(spyPostMessage).toHaveBeenCalledWith(
expect.objectContaining({
Expand Down Expand Up @@ -239,7 +239,7 @@ describe('Relying Party', () => {

expect(relyingParty).toBeInstanceOf(RelyingParty);

expect(window.open).toHaveBeenCalledTimes(1);
expect(window.open).toHaveBeenCalledOnce();
expect(window.close).not.toHaveBeenCalled();
});

Expand All @@ -251,8 +251,8 @@ describe('Relying Party', () => {
RelyingParty.connect(mockParameters).catch((err: Error) => {
expect(err.message).toBe('Connection timeout. Unable to connect to the signer.');

expect(window.open).toHaveBeenCalledTimes(1);
expect(window.close).toHaveBeenCalledTimes(1);
expect(window.open).toHaveBeenCalledOnce();
expect(window.close).toHaveBeenCalledOnce();

vi.useRealTimers();

Expand All @@ -274,7 +274,7 @@ describe('Relying Party', () => {

const {disconnect} = await promise;

expect(window.open).toHaveBeenCalledTimes(1);
expect(window.open).toHaveBeenCalledOnce();
expect(window.close).not.toHaveBeenCalled();

await disconnect();
Expand All @@ -291,7 +291,7 @@ describe('Relying Party', () => {

await connectAndDisconnect();

expect(clearIntervalSpy).toHaveBeenCalledTimes(1);
expect(clearIntervalSpy).toHaveBeenCalledOnce();

clearIntervalSpy.mockRestore();
});
Expand All @@ -301,7 +301,7 @@ describe('Relying Party', () => {

await connectAndDisconnect(spyOnDisconnect);

expect(spyOnDisconnect).toHaveBeenCalledTimes(1);
expect(spyOnDisconnect).toHaveBeenCalledOnce();
});
});
});
Expand Down Expand Up @@ -382,7 +382,7 @@ describe('Relying Party', () => {
`Request to signer timed out after ${RELYING_PARTY_TIMEOUT_REQUEST_SUPPORTED_STANDARD} milliseconds.`
);

expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledOnce();

vi.useRealTimers();

Expand Down Expand Up @@ -505,8 +505,8 @@ describe('Relying Party', () => {

await promise;

expect(spy).toHaveBeenCalledTimes(1);
expect(spyPostMessage).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledOnce();
expect(spyPostMessage).toHaveBeenCalledOnce();

expect(spyPostMessage).toHaveBeenCalledWith(
expect.objectContaining({
Expand Down Expand Up @@ -620,7 +620,7 @@ describe('Relying Party', () => {
`Request to signer timed out after ${RELYING_PARTY_TIMEOUT_PERMISSIONS} milliseconds.`
);

expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledOnce();

vi.useRealTimers();

Expand Down Expand Up @@ -743,8 +743,8 @@ describe('Relying Party', () => {

await promise;

expect(spy).toHaveBeenCalledTimes(1);
expect(spyPostMessage).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledOnce();
expect(spyPostMessage).toHaveBeenCalledOnce();

expect(spyPostMessage).toHaveBeenCalledWith(
expect.objectContaining({
Expand Down Expand Up @@ -839,7 +839,7 @@ describe('Relying Party', () => {
`Request to signer timed out after ${RELYING_PARTY_TIMEOUT_REQUEST_PERMISSIONS} milliseconds.`
);

expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledOnce();

vi.useRealTimers();

Expand Down Expand Up @@ -962,8 +962,8 @@ describe('Relying Party', () => {

await promise;

expect(spy).toHaveBeenCalledTimes(1);
expect(spyPostMessage).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledOnce();
expect(spyPostMessage).toHaveBeenCalledOnce();

expect(spyPostMessage).toHaveBeenCalledWith(
expect.objectContaining({
Expand All @@ -990,8 +990,8 @@ describe('Relying Party', () => {

await promise;

expect(spy).toHaveBeenCalledTimes(1);
expect(spyPostMessage).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledOnce();
expect(spyPostMessage).toHaveBeenCalledOnce();

expect(spyPostMessage).toHaveBeenCalledWith(
expect.objectContaining({
Expand Down Expand Up @@ -1101,7 +1101,7 @@ describe('Relying Party', () => {
`Request to signer timed out after ${RELYING_PARTY_TIMEOUT_ACCOUNTS} milliseconds.`
);

expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledOnce();

vi.useRealTimers();

Expand Down Expand Up @@ -1224,8 +1224,8 @@ describe('Relying Party', () => {

await promise;

expect(spy).toHaveBeenCalledTimes(1);
expect(spyPostMessage).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledOnce();
expect(spyPostMessage).toHaveBeenCalledOnce();

expect(spyPostMessage).toHaveBeenCalledWith(
expect.objectContaining({
Expand Down Expand Up @@ -1361,7 +1361,7 @@ describe('Relying Party', () => {
`Request to signer timed out after ${RELYING_PARTY_TIMEOUT_CALL_CANISTER} milliseconds.`
);

expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledOnce();

vi.useRealTimers();

Expand Down Expand Up @@ -1494,8 +1494,8 @@ describe('Relying Party', () => {

await promise;

expect(spy).toHaveBeenCalledTimes(1);
expect(spyPostMessage).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledOnce();
expect(spyPostMessage).toHaveBeenCalledOnce();

expect(spyPostMessage).toHaveBeenCalledWith(
expect.objectContaining({
Expand Down Expand Up @@ -1530,7 +1530,7 @@ describe('Relying Party', () => {

const callResult = await promise;

expect(spyAssertCallResponse).toHaveBeenCalledTimes(1);
expect(spyAssertCallResponse).toHaveBeenCalledOnce();

expect(spyAssertCallResponse).toHaveBeenCalledWith({
result: callResult,
Expand Down
2 changes: 1 addition & 1 deletion src/services/signer.services.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ describe('Signer services', () => {
prompt
});

expect(spySignerApiCall).toHaveBeenCalledTimes(1);
expect(spySignerApiCall).toHaveBeenCalledOnce();

expect(spySignerApiCall).toHaveBeenCalledWith({
...signerOptions,
Expand Down
18 changes: 9 additions & 9 deletions src/signer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ describe('Signer', () => {
const messageEvent = new MessageEvent('message', requestPermissionsMsg);
window.dispatchEvent(messageEvent);

expect(postMessageMock).toHaveBeenCalledTimes(1);
expect(postMessageMock).toHaveBeenCalledOnce();

expect(postMessageMock).toHaveBeenCalledWith(
{
Expand All @@ -825,7 +825,7 @@ describe('Signer', () => {
const messageEvent = new MessageEvent('message', requestPermissionsMsg);
window.dispatchEvent(messageEvent);

expect(promptSpy).toHaveBeenCalledTimes(1);
expect(promptSpy).toHaveBeenCalledOnce();

expect(promptSpy).toHaveBeenCalledWith({
requestedScopes: requestPermissionsDataSortedScopes.map((scope) => ({
Expand All @@ -850,7 +850,7 @@ describe('Signer', () => {
const messageEvent = new MessageEvent('message', requestPermissionsMsg);
window.dispatchEvent(messageEvent);

expect(promptSpy).toHaveBeenCalledTimes(1);
expect(promptSpy).toHaveBeenCalledOnce();

expect(promptSpy).toHaveBeenCalledWith({
requestedScopes: requestPermissionsDataSortedScopes.map((scope) => ({
Expand Down Expand Up @@ -889,7 +889,7 @@ describe('Signer', () => {
const messageEvent = new MessageEvent('message', requestPermissionsMsg);
window.dispatchEvent(messageEvent);

expect(promptSpy).toHaveBeenCalledTimes(1);
expect(promptSpy).toHaveBeenCalledOnce();

expect(promptSpy).toHaveBeenCalledWith({
requestedScopes: requestPermissionsDataSortedScopes.map((scope) => ({
Expand Down Expand Up @@ -1107,7 +1107,7 @@ describe('Signer', () => {
}
]);

await vi.waitFor(() => expect(spy).toHaveBeenCalledTimes(1));
await vi.waitFor(() => expect(spy).toHaveBeenCalledOnce());
});
});
});
Expand Down Expand Up @@ -1650,7 +1650,7 @@ describe('Signer', () => {

approve?.(mockAccounts);

await vi.waitFor(() => expect(spy).toHaveBeenCalledTimes(1));
await vi.waitFor(() => expect(spy).toHaveBeenCalledOnce());
});
});
});
Expand Down Expand Up @@ -1886,7 +1886,7 @@ describe('Signer', () => {
});
});

expect(promptSpy).not.toHaveBeenCalledTimes(1);
expect(promptSpy).not.toHaveBeenCalledOnce();
});
});
});
Expand Down Expand Up @@ -2085,7 +2085,7 @@ describe('Signer', () => {
});

it('should call canister and notify success', () => {
expect(spyCanisterCall).toHaveBeenCalledTimes(1);
expect(spyCanisterCall).toHaveBeenCalledOnce();

expect(spyCanisterCall).toHaveBeenCalledWith({
...signerOptions,
Expand Down Expand Up @@ -2123,7 +2123,7 @@ describe('Signer', () => {
});

it('should call canister and notify error', () => {
expect(spyCanisterCall).toHaveBeenCalledTimes(1);
expect(spyCanisterCall).toHaveBeenCalledOnce();

expect(spyCanisterCall).toHaveBeenCalledWith({
...signerOptions,
Expand Down
4 changes: 2 additions & 2 deletions src/utils/timeout.utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('retryUntilReady', () => {
const result = await retryUntilReady({retries: 3, isReady, fn});

expect(result).toBe('ready');
expect(isReady).toHaveBeenCalledTimes(1);
expect(isReady).toHaveBeenCalledOnce();
expect(fn).not.toHaveBeenCalled();
});

Expand All @@ -49,7 +49,7 @@ describe('retryUntilReady', () => {
const result = await retryUntilReady({retries: 3, isReady, fn});

expect(result).toBe('error');
expect(isReady).toHaveBeenCalledTimes(1);
expect(isReady).toHaveBeenCalledOnce();
expect(fn).not.toHaveBeenCalled();
});

Expand Down

0 comments on commit b49f541

Please sign in to comment.