Skip to content

Commit

Permalink
feat: canister api to call consent message
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpeterparker committed Sep 3, 2024
1 parent ed57747 commit fdfec62
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/api/canister.api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type {Agent} from '@dfinity/agent';
import type {Principal} from '@dfinity/principal';
import type {
icrc21_consent_message_request,
icrc21_consent_message_response
} from '../declarations/icrc-21';
import {getIcrc21Actor} from './actors.api';

export const consentMessage = async ({
agent,
canisterId,
request
}: {
agent: Agent;
canisterId: string | Principal;
request: icrc21_consent_message_request;
}): Promise<icrc21_consent_message_response> => {
const {icrc21_canister_call_consent_message: canisterCallConsentMessage} = await getIcrc21Actor({
agent,
canisterId
});
return await canisterCallConsentMessage(request);
};
92 changes: 92 additions & 0 deletions src/api/caniter.api.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import {HttpAgent} from '@dfinity/agent';
import {mockCanisterId} from '../constants/icrc-accounts.mocks';
import type {_SERVICE as Icrc21Actor} from '../declarations/icrc-21';
import {

Check failure on line 4 in src/api/caniter.api.spec.ts

View workflow job for this annotation

GitHub Actions / lint

All imports in the declaration are only used as types. Use `import type`
icrc21_consent_message_request,
icrc21_consent_message_response
} from '../declarations/icrc-21';
import * as actor from './actors.api';
import {consentMessage} from './canister.api';

vi.mock('@dfinity/agent', async (importOriginal) => {
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
const originalModule = await importOriginal<typeof import('@dfinity/agent')>();

const mockActor = {test: 123};

return {
...originalModule,
Actor: {
...originalModule.Actor,
createActor: vi.fn().mockResolvedValue(mockActor)
},
createSync: vi.fn()
};
});

describe('canister.api', () => {
const agent = HttpAgent.createSync();

describe('consentMessage', () => {
const consentMessageRequest: icrc21_consent_message_request = {
method: 'icrc1_transfer',
arg: new Uint8Array([1, 2, 3]),
user_preferences: {
metadata: {
language: 'en-US',
utc_offset_minutes: []
},
device_spec: [
{
GenericDisplay: null
}
]
}
};

const consentMessageResponse: icrc21_consent_message_response = {
Ok: {
consent_message: {
GenericDisplayMessage: 'Transfer 1 ICP to account abcd'
},
metadata: {
language: 'en-US',
utc_offset_minutes: []
}
}
};

it('should call the canisterCallConsentMessage with correct arguments and return the result', async () => {
const mockIcrc21Actor: Icrc21Actor = {
icrc10_supported_standards: vi
.fn()
.mockResolvedValue([
{url: 'http://example.com', name: 'Example'}
]) as unknown as Icrc21Actor['icrc10_supported_standards'],
icrc21_canister_call_consent_message: vi
.fn()
.mockImplementation(async (_request: icrc21_consent_message_request) => {
return consentMessageResponse;
}) as unknown as Icrc21Actor['icrc21_canister_call_consent_message']
};

const spy = vi.spyOn(actor, 'getIcrc21Actor');
spy.mockImplementation(async () => {
return mockIcrc21Actor;
});

const result = await consentMessage({
agent,
canisterId: mockCanisterId,
request: consentMessageRequest
});

expect(spy).toHaveBeenCalledWith({
agent,
canisterId: mockCanisterId
});
expect(mockIcrc21Actor.icrc21_canister_call_consent_message).toHaveBeenCalledWith(consentMessageRequest);

Check failure on line 88 in src/api/caniter.api.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `consentMessageRequest` with `⏎········consentMessageRequest⏎······`
expect(result).toBe(consentMessageResponse);
});
});
});

0 comments on commit fdfec62

Please sign in to comment.