diff --git a/CHANGELOG.md b/CHANGELOG.md index 64f6277..9849cf8 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.28.22](https://github.com/Fantom-foundation/Fantom-Vue3-Components/compare/v0.28.21...v0.28.22) (2022-11-10) + + +### Features + +* **GqlApi:** adjust `onDone` returned data ([974e942](https://github.com/Fantom-foundation/Fantom-Vue3-Components/commit/974e94253ec9e660e6dd8ef6bf4557a79007aedb)) + ### [0.28.21](https://github.com/Fantom-foundation/Fantom-Vue3-Components/compare/v0.28.20...v0.28.21) (2022-11-10) diff --git a/package-lock.json b/package-lock.json index 1a07424..72c3577 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "fantom-vue3-components", - "version": "0.28.21", + "version": "0.28.22", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "fantom-vue3-components", - "version": "0.28.21", + "version": "0.28.22", "license": "MIT", "dependencies": { "@vueuse/core": "^8.5.0", diff --git a/package.json b/package.json index 5b59277..c5e71fe 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fantom-vue3-components", - "version": "0.28.21", + "version": "0.28.22", "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 6e09be9..e58a8a8 100644 --- a/src/plugins/api/GqlApi/GqlApi.js +++ b/src/plugins/api/GqlApi/GqlApi.js @@ -108,7 +108,7 @@ export class GqlApi extends WebApi { fnName = '', }) { const enabled = ref(!disabled); - const { result, loading, error, refetch, fetchMore, onResult, onError } = GqlApi.#useQueryMock({ + const { result, loading, error, refetch, fetchMore, onResult, onError } = this.#useQueryMock({ mockFunction: this._getFunctionMock(mockFunction, fnName), errors, enabled, @@ -141,9 +141,11 @@ export class GqlApi extends WebApi { fnName = '', }) { const _called = ref(false); - const { mutate, loading, error, called, onDone, onError } = GqlApi.#useMutaionMock({ + const { mutate, loading, error, called, onDone, onError } = this.#useMutaionMock({ mockFunction: this._getFunctionMock(mockFunction, fnName), errors, + pickFn, + defaultData, _called, }); @@ -160,7 +162,7 @@ export class GqlApi extends WebApi { }; } - static #useQueryMock({ mockFunction, delay = 0, errors = [], enabled = ref(true) }) { + #useQueryMock({ mockFunction, delay = 0, errors = [], enabled = ref(true) }) { const result = ref(null); const loading = ref(true); const error = ref(null); @@ -222,7 +224,7 @@ export class GqlApi extends WebApi { }; } - static #useMutaionMock({ mockFunction, delay = 0, errors = [], _called = ref(false) }) { + #useMutaionMock({ mockFunction, delay = 0, errors = [], pickFn, defaultData, _called = ref(false) }) { let result; const called = ref(_called.value); const loading = ref(true); @@ -230,6 +232,24 @@ export class GqlApi extends WebApi { let onDoneFunction = null; let onErrorFunction = null; + const onDefer = (...args) => { + loading.value = false; + + if (errors.length > 0) { + error.value = { errors }; + + if (typeof onErrorFunction === 'function') { + onErrorFunction({ errors }); + } + } else { + result = mockFunction(...args); + + if (typeof onDoneFunction === 'function') { + onDoneFunction(this._useResult({ value: result?.data || result }, defaultData, pickFn)); + } + } + }; + function onDone(fn) { onDoneFunction = fn; } @@ -247,26 +267,6 @@ export class GqlApi extends WebApi { }, delay); } - function onDefer(...args) { - loading.value = false; - - if (errors.length > 0) { - error.value = { errors }; - - if (typeof onErrorFunction === 'function') { - onErrorFunction({ errors }); - } - } else { - result = mockFunction(...args); - - if (typeof onDoneFunction === 'function') { - onDoneFunction({ - data: result, - }); - } - } - } - // defer(onDefer, delay); return { diff --git a/src/plugins/api/GqlApi/GqlApi.spec.js b/src/plugins/api/GqlApi/GqlApi.spec.js index 0dffbcf..7564fe6 100644 --- a/src/plugins/api/GqlApi/GqlApi.spec.js +++ b/src/plugins/api/GqlApi/GqlApi.spec.js @@ -233,11 +233,7 @@ describe('GqlApi', () => { await delay(); - expect(spyOnDone).toBeCalledWith({ - data: { - myFoo: 'foo2', - }, - }); + expect(spyOnDone).toBeCalledWith('foo2'); expect(result.loading.value).toBe(false); expect(result.called.value).toBe(true); }); @@ -289,11 +285,7 @@ describe('GqlApi', () => { mutate({}); await delay(); - expect(result).toEqual({ - data: { - myFoo: 'data fake', - }, - }); + expect(result).toBe('data fake'); api.restoreDataFake('fooMock'); });