Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose error #72

Merged
merged 1 commit into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"devDependencies": {
"@feathersjs/adapter-commons": "5.0.0-pre.15",
"@feathersjs/feathers": "5.0.0-pre.15",
"@feathersjs/errors": "5.0.0-pre.15",
"@geprog/eslint-config": "1.0.2",
"@geprog/semantic-release-config": "1.0.0",
"@jest/types": "27.4.2",
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 15 additions & 5 deletions src/useFind.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { FeathersError } from '@feathersjs/errors';
import type { Application, FeathersService, Params, ServiceMethods } from '@feathersjs/feathers';
import sift from 'sift';
import { getCurrentInstance, onBeforeUnmount, Ref, ref, watch } from 'vue';
Expand Down Expand Up @@ -73,6 +74,7 @@ function loadServiceEventHandlers<
export type UseFind<T> = {
data: Ref<T[]>;
isLoading: Ref<boolean>;
error: Ref<FeathersError | undefined>;
unload: () => void;
};

Expand All @@ -94,22 +96,30 @@ export default <CustomApplication extends Application>(feathers: CustomApplicati
// type cast is fine here (source: https://github.com/vuejs/vue-next/issues/2136#issuecomment-693524663)
const data = ref<M[]>([]) as Ref<M[]>;
const isLoading = ref(false);
const error = ref<FeathersError>();

const service = feathers.service(serviceName as string);
const unloadEventHandlers = loadServiceEventHandlers(service, params, data);

const find = async () => {
isLoading.value = true;
error.value = undefined;

if (!params.value) {
data.value = [];
isLoading.value = false;
return;
}

// TODO: the typecast below is necessary due to the prerelease state of feathers v5. The problem there is
// that the AdapterService interface is not yet updated and is not compatible with the ServiceMethods interface.
const res = await (service as unknown as ServiceMethods<M>).find(params.value);
data.value = Array.isArray(res) ? res : [res];
try {
// TODO: the typecast below is necessary due to the prerelease state of feathers v5. The problem there is
// that the AdapterService interface is not yet updated and is not compatible with the ServiceMethods interface.
const res = await (service as unknown as ServiceMethods<M>).find(params.value);
data.value = Array.isArray(res) ? res : [res];
} catch (_error) {
error.value = _error as FeathersError;
}

isLoading.value = false;
};

Expand All @@ -129,5 +139,5 @@ export default <CustomApplication extends Application>(feathers: CustomApplicati
onBeforeUnmount(unload);
}

return { data, isLoading, unload };
return { data, isLoading, unload, error };
};
19 changes: 15 additions & 4 deletions src/useGet.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { FeathersError } from '@feathersjs/errors';
import type { Application, FeathersService, Id, Params, ServiceMethods } from '@feathersjs/feathers';
import { getCurrentInstance, onBeforeUnmount, Ref, ref, watch } from 'vue';

Expand Down Expand Up @@ -56,6 +57,7 @@ function loadServiceEventHandlers<
export type UseGet<T> = {
data: Ref<T | undefined>;
isLoading: Ref<boolean>;
error: Ref<FeathersError | undefined>;
unload: () => void;
};

Expand All @@ -77,21 +79,30 @@ export default <CustomApplication extends Application>(feathers: CustomApplicati
): UseGet<M> => {
const data = ref<M>();
const isLoading = ref(false);
const error = ref<FeathersError>();

const service = feathers.service(serviceName as string);

const unloadEventHandlers = loadServiceEventHandlers(service, _id, data);

const get = async () => {
isLoading.value = true;
error.value = undefined;

if (!_id.value) {
data.value = undefined;
isLoading.value = false;
return;
}
// TODO: the typecast below is necessary due to the prerelease state of feathers v5. The problem there is
// that the AdapterService interface is not yet updated and is not compatible with the ServiceMethods interface.
data.value = await (service as unknown as ServiceMethods<M>).get(_id.value, params.value);

try {
// TODO: the typecast below is necessary due to the prerelease state of feathers v5. The problem there is
// that the AdapterService interface is not yet updated and is not compatible with the ServiceMethods interface.
data.value = await (service as unknown as ServiceMethods<M>).get(_id.value, params.value);
} catch (_error) {
error.value = _error as FeathersError;
}

isLoading.value = false;
};

Expand All @@ -111,5 +122,5 @@ export default <CustomApplication extends Application>(feathers: CustomApplicati
onBeforeUnmount(unload);
}

return { isLoading, data, unload };
return { isLoading, data, error, unload };
};
82 changes: 81 additions & 1 deletion test/useFind.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { FeathersError, GeneralError } from '@feathersjs/errors';
import type { Application, Params } from '@feathersjs/feathers';
import { nextTick, ref } from 'vue';

Expand Down Expand Up @@ -107,7 +108,7 @@ describe('Find composition', () => {
});

it('should indicate data finished loading', async () => {
expect.assertions(2);
expect.assertions(3);

// given
let serviceFindPromiseResolve: (value: TestModel[] | PromiseLike<TestModel[]>) => void = jest.fn();
Expand All @@ -132,12 +133,53 @@ describe('Find composition', () => {
findComposition = useFind('testModels');
});

// before when
expect(findComposition).toBeTruthy();
expect(findComposition && findComposition.isLoading.value).toBeTruthy();

// when
serviceFindPromiseResolve(testModels);
await nextTick();

// then
expect(findComposition && findComposition.isLoading.value).toBeFalsy();
});

it('should indicate data finished loading even if an error occurred', async () => {
expect.assertions(3);

// given
let serviceFindPromiseReject: (reason: FeathersError) => void = jest.fn();
const serviceFind = jest.fn(
() =>
new Promise<TestModel[]>((resolve, reject) => {
serviceFindPromiseReject = reject;
}),
);
const feathersMock = {
service: () => ({
find: serviceFind,
on: jest.fn(),
off: jest.fn(),
}),
on: jest.fn(),
off: jest.fn(),
} as unknown as Application;
const useFind = useFindOriginal(feathersMock);
let findComposition = null as UseFind<TestModel> | null;
mountComposition(() => {
findComposition = useFind('testModels');
});

// before when
expect(findComposition).toBeTruthy();
expect(findComposition && findComposition.isLoading.value).toBeTruthy();

// when
serviceFindPromiseReject(new GeneralError('test error'));
await nextTick();

// then
expect(findComposition && findComposition.isLoading.value).toBeFalsy();
});

Expand Down Expand Up @@ -341,6 +383,44 @@ describe('Find composition', () => {
expect(findComposition && findComposition.data.value).toStrictEqual([additionalTestModel]);
});

it('should set an error if something failed', async () => {
expect.assertions(3);

// given
let serviceFindPromiseReject: (reason: FeathersError) => void = jest.fn();
const serviceFind = jest.fn(
() =>
new Promise<TestModel[]>((resolve, reject) => {
serviceFindPromiseReject = reject;
}),
);
const feathersMock = {
service: () => ({
find: serviceFind,
on: jest.fn(),
off: jest.fn(),
}),
on: jest.fn(),
off: jest.fn(),
} as unknown as Application;
const useFind = useFindOriginal(feathersMock);
let findComposition = null as UseFind<TestModel> | null;
mountComposition(() => {
findComposition = useFind('testModels');
});

// before when
expect(findComposition).toBeTruthy();
expect(findComposition && findComposition.error.value).toBeFalsy();

// when
serviceFindPromiseReject(new GeneralError('test error'));
await nextTick();

// then
expect(findComposition && findComposition.error.value).toBeTruthy();
});

describe('Event Handlers', () => {
it('should listen to "create" events', () => {
expect.assertions(2);
Expand Down
82 changes: 81 additions & 1 deletion test/useGet.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { FeathersError, GeneralError } from '@feathersjs/errors';
import type { Application } from '@feathersjs/feathers';
import { nextTick, ref } from 'vue';

Expand Down Expand Up @@ -104,7 +105,7 @@ describe('Get composition', () => {
});

it('should indicate data finished loading', async () => {
expect.assertions(2);
expect.assertions(3);

// given
let serviceGetPromiseResolve: (value: TestModel | PromiseLike<TestModel>) => void = jest.fn();
Expand All @@ -129,12 +130,53 @@ describe('Get composition', () => {
getComposition = useGet('testModels', ref(testModel._id));
});

// before when
expect(getComposition).toBeTruthy();
expect(getComposition && getComposition.isLoading.value).toBeTruthy();

// when
serviceGetPromiseResolve(testModel);
await nextTick();

// then
expect(getComposition && getComposition.isLoading.value).toBeFalsy();
});

it('should indicate data finished loading even if an error occurred', async () => {
expect.assertions(3);

// given
let serviceGetPromiseReject: (reason: FeathersError) => void = jest.fn();
const serviceGet = jest.fn(
() =>
new Promise<TestModel>((resolve, reject) => {
serviceGetPromiseReject = reject;
}),
);
const feathersMock = {
service: () => ({
get: serviceGet,
on: jest.fn(),
off: jest.fn(),
}),
on: jest.fn(),
off: jest.fn(),
} as unknown as Application;
const useGet = useGetOriginal(feathersMock);
let getComposition = null as UseGet<TestModel> | null;
mountComposition(() => {
getComposition = useGet('testModels', ref(testModel._id));
});

// before when
expect(getComposition).toBeTruthy();
expect(getComposition && getComposition.isLoading.value).toBeTruthy();

// when
serviceGetPromiseReject(new GeneralError('test error'));
await nextTick();

// then
expect(getComposition && getComposition.isLoading.value).toBeFalsy();
});

Expand Down Expand Up @@ -260,6 +302,44 @@ describe('Get composition', () => {
expect(getComposition && getComposition.data.value).toStrictEqual(additionalTestModel);
});

it('should set an error if something failed', async () => {
expect.assertions(3);

// given
let serviceGetPromiseReject: (reason: FeathersError) => void = jest.fn();
const serviceGet = jest.fn(
() =>
new Promise<TestModel>((resolve, reject) => {
serviceGetPromiseReject = reject;
}),
);
const feathersMock = {
service: () => ({
get: serviceGet,
on: jest.fn(),
off: jest.fn(),
}),
on: jest.fn(),
off: jest.fn(),
} as unknown as Application;
const useGet = useGetOriginal(feathersMock);
let getComposition = null as UseGet<TestModel> | null;
mountComposition(() => {
getComposition = useGet('testModels', ref(testModel._id));
});

// before when
expect(getComposition).toBeTruthy();
expect(getComposition && getComposition.error.value).toBeFalsy();

// when
serviceGetPromiseReject(new GeneralError('test error'));
await nextTick();

// then
expect(getComposition && getComposition.error.value).toBeTruthy();
});

describe('Event Handlers', () => {
it('should listen to "create" events', async () => {
expect.assertions(3);
Expand Down