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

[BUGFIX] Fix test setup #48

Merged
merged 3 commits into from
Aug 4, 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
4 changes: 2 additions & 2 deletions .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"commit": false,
"linked": [],
"access": "restricted",
"baseBranch": "master",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"ignore": []
}
}
5 changes: 5 additions & 0 deletions .changeset/tiny-sloths-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'ts-japi': patch
---

Fixes the test framework
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "A highly-modular (typescript-friendly)-framework agnostic library for serializing data to the JSON:API specification",
"main": "lib/index.js",
"scripts": {
"test": " jest --runInBand --verbose --coverage",
"test": "jest --runInBand --verbose --coverage",
"playground": "ts-node ./benchmarks/playground.benchmark",
"lint": "eslint .",
"examples": "ts-node ./examples/",
Expand Down
35 changes: 21 additions & 14 deletions src/models/error.model.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
import { Dictionary, nullish } from '..';
import { ErrorOptions } from '../interfaces/error.interface';
import { isObject } from '../utils/is-object';
import Link from './link.model';
import Meta from './meta.model';
import { isObject } from '../utils/is-object';

export default class JapiError {
/**
* Tests whether `error` has similar attributes to a JapiError
*
* @param error - An unknown object
* @param error An unknown object
*/
public static isLikeJapiError(error: unknown): error is Partial<JapiError> {
console.log(!isObject(error));
if (!isObject(error)) return false;
return (
['id', 'status', 'code', 'title', 'detail', 'source', 'links', 'meta'].some(
(attrName) => attrName in error
) &&
[
(['id', 'status', 'code', 'title', 'detail'] as const).every(
(attrName) => !(attrName in error) || typeof error[attrName] === 'string'
),
(['source', 'links', 'meta'] as const).every(
(attrName) => !(attrName in error) || isObject(error[attrName])
),
].every((v) => v)
const hasErrorKeys = [
'id',
'status',
'code',
'title',
'detail',
'source',
'links',
'meta',
].some((attrName) => attrName in error);
const expectedStringKeys = (['id', 'status', 'code', 'title', 'detail'] as const).every(
(attrName) =>
!(attrName in error) || error[attrName] === undefined || typeof error[attrName] === 'string'
);
const expectedObjectKeys = (['source', 'links', 'meta'] as const).every(
(attrName) =>
!(attrName in error) || error[attrName] === undefined || isObject(error[attrName])
);
return hasErrorKeys && [expectedStringKeys, expectedObjectKeys].every((v) => v);
}

/** @internal */
Expand Down
134 changes: 69 additions & 65 deletions test/utils/model-factory.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,74 @@
import { findAllExisting } from "./find-all-existing";
import { capitalize, camelCase } from "lodash";
import { pushIfNotExists } from "./push-if-not-exists";
import Base from "../models/base.model";
import { camelCase, capitalize } from 'lodash';
import Base from '../models/base.model';
import { findAllExisting } from './find-all-existing';
import { pushIfNotExists } from './push-if-not-exists';

const ModelFactory = {
addArrayAttribute<T extends typeof Base, U extends typeof Base>(
name: string,
target: T,
source: U
) {
const getterName = `get${capitalize(camelCase(name))}`;
target.afterRemoveHook = target.afterRemoveHook ?? [];
target.beforeSaveHook = target.beforeSaveHook ?? [];
target.prototype = target.prototype ?? ({} as any);
(target.prototype as any)[getterName] = function <Target>(this: Target) {
return findAllExisting((this as any)[name], (id: string) => source.find(id));
};
target.afterRemoveHook.push(<Target, Source extends Base>(model: Target) => {
(model as any)[getterName]().map((m: Source) => source.remove(m));
});
target.beforeSaveHook.push(<Target>(model: Target) => {
findAllExisting((model as any)[name], (id: string) => source.find(id));
});
},
addSingleAttribute<T extends typeof Base, U extends typeof Base>(
name: string,
othername: string,
target: T,
source: U
) {
const getterName = `get${capitalize(camelCase(name))}`;
target.beforeSaveHook = target.beforeSaveHook ?? [];
target.prototype = target.prototype ?? ({} as any);
(target.prototype as any)[getterName] = function <Target>(this: Target) {
return source.find((this as any)[name]);
};
target.beforeSaveHook.push(<Target extends Base>(model: Target) => {
const sourceModel = (model as any)[getterName]();
if (!sourceModel) throw new Error(`no ${name}`);
pushIfNotExists(sourceModel[othername], model.id, (id) => id === model.id);
});
},
createModel<T extends typeof Base>(model: T) {
model.storage = [];
model.find = function (this: T, id: string) {
if (this.beforeFindHook) this.beforeFindHook.forEach((hook) => hook(id));
const result = this.storage.find((u) => u.id === id);
if (this.afterFindHook) this.afterFindHook.forEach((hook) => hook(id));
return result;
};
model.remove = function <U extends Base>(this: T, obj: U) {
if (this.beforeRemoveHook) this.beforeRemoveHook.forEach((hook) => hook(obj));
let idx = this.storage.findIndex((u) => u.id === obj.id);
if (typeof idx === "number") {
delete this.storage[idx];
}
if (this.afterRemoveHook) this.afterRemoveHook.forEach((hook) => hook(obj));
return obj;
};
model.save = function <U extends Base>(this: T, model: U) {
if (this.beforeSaveHook) this.beforeSaveHook.forEach((hook) => hook(model));
pushIfNotExists(this.storage, model, (m) => m.id === model.id);
if (this.afterSaveHook) this.afterSaveHook.forEach((hook) => hook(model));
return model;
};
},
addArrayAttribute<T extends typeof Base, U extends typeof Base>(
name: string,
target: T,
source: U
) {
const getterName = `get${capitalize(camelCase(name))}`;
target.afterRemoveHook = target.afterRemoveHook ?? [];
target.beforeSaveHook = target.beforeSaveHook ?? [];
if (!target.prototype) {
target.prototype = {} as any;
}
(target.prototype as any)[getterName] = function <Target>(this: Target) {
return findAllExisting((this as any)[name], (id: string) => source.find(id));
};
target.afterRemoveHook.push(<Target, Source extends Base>(model: Target) => {
(model as any)[getterName]().map((m: Source) => source.remove(m));
});
target.beforeSaveHook.push(<Target>(model: Target) => {
findAllExisting((model as any)[name], (id: string) => source.find(id));
});
},
addSingleAttribute<T extends typeof Base, U extends typeof Base>(
name: string,
othername: string,
target: T,
source: U
) {
const getterName = `get${capitalize(camelCase(name))}`;
target.beforeSaveHook = target.beforeSaveHook ?? [];
if (!target.prototype) {
target.prototype = {} as any;
}
(target.prototype as any)[getterName] = function <Target>(this: Target) {
return source.find((this as any)[name]);
};
target.beforeSaveHook.push(<Target extends Base>(model: Target) => {
const sourceModel = (model as any)[getterName]();
if (!sourceModel) throw new Error(`no ${name}`);
pushIfNotExists(sourceModel[othername], model.id, (id) => id === model.id);
});
},
createModel<T extends typeof Base>(model: T) {
model.storage = [];
model.find = function (this: T, id: string) {
if (this.beforeFindHook) this.beforeFindHook.forEach((hook) => hook(id));
const result = this.storage.find((u) => u.id === id);
if (this.afterFindHook) this.afterFindHook.forEach((hook) => hook(id));
return result;
};
model.remove = function <U extends Base>(this: T, obj: U) {
if (this.beforeRemoveHook) this.beforeRemoveHook.forEach((hook) => hook(obj));
let idx = this.storage.findIndex((u) => u.id === obj.id);
if (typeof idx === 'number') {
delete this.storage[idx];
}
if (this.afterRemoveHook) this.afterRemoveHook.forEach((hook) => hook(obj));
return obj;
};
model.save = function <U extends Base>(this: T, model: U) {
if (this.beforeSaveHook) this.beforeSaveHook.forEach((hook) => hook(model));
pushIfNotExists(this.storage, model, (m) => m.id === model.id);
if (this.afterSaveHook) this.afterSaveHook.forEach((hook) => hook(model));
return model;
};
},
};

export default ModelFactory;