diff --git a/CHANGELOG.md b/CHANGELOG.md index e392353..9fe5603 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +### [0.32.4](https://github.com/Fantom-foundation/Fantom-Vue3-Components/compare/v0.32.3...v0.32.4) (2023-02-16) + + +### Features + +* **GqlApi:** add support for passing args to mock and fake functions ([45bb20a](https://github.com/Fantom-foundation/Fantom-Vue3-Components/commit/45bb20a0c4f5445afb3161471976cce12ae4a569)) + ### [0.32.3](https://github.com/Fantom-foundation/Fantom-Vue3-Components/compare/v0.32.2...v0.32.3) (2023-02-15) diff --git a/package-lock.json b/package-lock.json index f0b9189..8e1eb7d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "fantom-vue3-components", - "version": "0.32.3", + "version": "0.32.4", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "fantom-vue3-components", - "version": "0.32.3", + "version": "0.32.4", "license": "MIT", "dependencies": { "@vueuse/core": "^8.5.0", diff --git a/package.json b/package.json index f0e6362..64b2c44 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fantom-vue3-components", - "version": "0.32.3", + "version": "0.32.4", "description": "Library of various Vue 3 components with focus on accessibility", "author": "Fantom Foundation", "license": "MIT", diff --git a/src/plugins/api/GqlApi/GqlApi.js b/src/plugins/api/GqlApi/GqlApi.js index 599bb36..2a55488 100644 --- a/src/plugins/api/GqlApi/GqlApi.js +++ b/src/plugins/api/GqlApi/GqlApi.js @@ -109,10 +109,12 @@ export class GqlApi extends WebApi { errors = [], disabled = false, fnName = '', + args = [], }) { const enabled = ref(!disabled); const { result, loading, error, refetch, fetchMore, onResult, onError } = this.#useQueryMock({ mockFunction: this._getFunctionMock(mockFunction, fnName), + mockFunctionWithOrigArgs: () => this._getFunctionMock(mockFunction, fnName)(...args), errors, enabled, }); @@ -143,10 +145,12 @@ export class GqlApi extends WebApi { copyData = false, errors = [], fnName = '', + args = [], }) { const _called = ref(false); const { mutate, loading, error, called, onDone, onError } = this.#useMutaionMock({ mockFunction: this._getFunctionMock(mockFunction, fnName), + mockFunctionWithOrigArgs: () => this._getFunctionMock(mockFunction, fnName)(...args), errors, pickFn, copyData, @@ -168,7 +172,7 @@ export class GqlApi extends WebApi { }; } - #useQueryMock({ mockFunction, delay = 0, errors = [], enabled = ref(true) }) { + #useQueryMock({ mockFunction, mockFunctionWithOrigArgs, delay = 0, errors = [], enabled = ref(true) }) { const result = ref(null); const loading = ref(true); const error = ref(null); @@ -187,7 +191,7 @@ export class GqlApi extends WebApi { loading.value = true; defer(() => { - onDefer(...args); + onDefer(false, ...args); }, delay); } @@ -196,7 +200,7 @@ export class GqlApi extends WebApi { refetch(...args); } - function onDefer(...args) { + function onDefer(origArgs, ...args) { loading.value = false; if (errors.length > 0) { @@ -206,7 +210,11 @@ export class GqlApi extends WebApi { onErrorFunction({ errors }); } } else if (enabled.value) { - result.value = mockFunction(...args); + if (origArgs) { + result.value = mockFunctionWithOrigArgs(); + } else { + result.value = mockFunction(...args); + } if (typeof onResultFunction === 'function') { onResultFunction({ @@ -216,7 +224,9 @@ export class GqlApi extends WebApi { } } - defer(onDefer, delay); + defer(() => { + onDefer(true); + }, delay); return { result, @@ -230,7 +240,16 @@ export class GqlApi extends WebApi { }; } - #useMutaionMock({ mockFunction, delay = 0, errors = [], pickFn, copyData, defaultData, _called = ref(false) }) { + #useMutaionMock({ + mockFunction, + mockFunctionWithOrigArgs, + delay = 0, + errors = [], + pickFn, + copyData, + defaultData, + _called = ref(false), + }) { let result; const called = ref(_called.value); const loading = ref(true); @@ -238,7 +257,7 @@ export class GqlApi extends WebApi { let onDoneFunction = null; let onErrorFunction = null; - const onDefer = (...args) => { + const onDefer = (origArgs, ...args) => { loading.value = false; if (errors.length > 0) { @@ -248,7 +267,11 @@ export class GqlApi extends WebApi { onErrorFunction({ errors }); } } else { - result = mockFunction(...args); + if (origArgs) { + result = mockFunctionWithOrigArgs(); + } else { + result = mockFunction(...args); + } if (typeof onDoneFunction === 'function') { onDoneFunction(this._useResult({ value: result?.data || result }, defaultData, pickFn, copyData)); @@ -269,11 +292,13 @@ export class GqlApi extends WebApi { called.value = true; defer(() => { - onDefer(...args); + onDefer(false, ...args); }, delay); } - // defer(onDefer, delay); + defer(() => { + onDefer(true); + }, delay); return { mutate, diff --git a/src/plugins/api/GqlApi/GqlApi.spec.js b/src/plugins/api/GqlApi/GqlApi.spec.js index 5191cab..0d82fc9 100644 --- a/src/plugins/api/GqlApi/GqlApi.spec.js +++ b/src/plugins/api/GqlApi/GqlApi.spec.js @@ -193,6 +193,45 @@ describe('GqlApi', () => { api.restoreDataFake('fooMock'); }); + + it('should pass the same arguments to the mock function as the original function', async () => { + const mockFn = vi.fn(); + function fooMock(...args) { + return gqlApi.queryMock({ + mockFunction: mockFn, + fnName: 'fooMock', + args: [...args], + }); + } + api.registerQueryMock(fooMock, 'fooMock'); + + api.query.fooMock({ arg: 'foo' }); + await delay(); + + expect(mockFn).toBeCalledWith({ arg: 'foo' }); + + api.restoreDataFake('fooMock'); + }); + + it('should pass the same arguments to the fake data function as the original function', async () => { + function fooMock(...args) { + return gqlApi.queryMock({ + mockFunction: () => {}, + fnName: 'fooMock', + args: [...args], + }); + } + api.registerQueryMock(fooMock, 'fooMock'); + const fakeDataFn = vi.fn(); + api.fakeData('fooMock', fakeDataFn); + + api.query.fooMock({ arg: 'foo' }); + await delay(); + + expect(fakeDataFn).toBeCalledWith({ arg: 'foo' }); + + api.restoreDataFake('fooMock'); + }); }); describe('mutationMock()', () => { @@ -301,5 +340,44 @@ describe('GqlApi', () => { api.restoreDataFake('fooMock'); }); + + it('should pass the same arguments to the mock function as the original function', async () => { + const mockFn = vi.fn(); + function fooMock(...args) { + return gqlApi.mutationMock({ + mockFunction: mockFn, + fnName: 'fooMock', + args: [...args], + }); + } + api.registerMutationMock(fooMock, 'fooMock'); + + api.mutation.fooMock({ arg: 'foo' }); + await delay(); + + expect(mockFn).toBeCalledWith({ arg: 'foo' }); + + api.restoreDataFake('fooMock'); + }); + + it('should pass the same arguments to the fake data function as the original function', async () => { + function fooMock(...args) { + return gqlApi.mutationMock({ + mockFunction: () => {}, + fnName: 'fooMock', + args: [...args], + }); + } + api.registerMutationMock(fooMock, 'fooMock'); + const fakeDataFn = vi.fn(); + api.fakeData('fooMock', fakeDataFn); + + api.query.fooMock({ arg: 'foo' }); + await delay(); + + expect(fakeDataFn).toBeCalledWith({ arg: 'foo' }); + + api.restoreDataFake('fooMock'); + }); }); });