Skip to content

Instantly share code, notes, and snippets.

@kasper573
Created October 7, 2018 23:55
Show Gist options
  • Save kasper573/f43c691b8f6561f87580c4c6af64e957 to your computer and use it in GitHub Desktop.
Save kasper573/f43c691b8f6561f87580c4c6af64e957 to your computer and use it in GitHub Desktop.
import {Crud} from 'lib/Crud';
import {FormTransportResponse} from 'lib/FormTransport';
import {User} from './models/User';
import {AppContext} from './AppContext';
import {createFixture} from '../lib/utils';
import {getDefaultModelSchema} from 'serializr';
const superAdmin = {
email: '[email protected]',
password: ''
};
export const integration = ({state, api}: AppContext) =>
new Test('Integration')
.and(
new Test('As Super Admin', () => state.auth.loginUser(superAdmin))
.and(
new CrudTest(api.user).withAll(),
new CrudTest(api.organisation).withAll(),
new CrudTest(api.enums.locales).withAll(),
new CrudTest(api.enums.priceTypes).withAll(),
new CrudTest(api.enums.currencies).withAll(),
new CrudTest(api.enums.countries).withAll(),
new CrudTest(api.enums.devices).withAll(),
new CrudTest(api.enums.devices).withAll(),
new CrudTest(api.enums.transactionTypes).withAll(),
new CrudTest(api.mediaKitObjects.categories).withAll(),
new CrudTest(api.mediaKitObjects.productTypes).withAll(),
new CrudTest(api.banners).withAll(),
new CrudTest(api.paymentPlans).withAll(),
new Test('Logout Super Admin', () => state.auth.logout()),
),
crudUser((user, password) => [
new Test('As Organisation Admin', () => state.auth.loginUser({password, email: user.email}))
.and(
new CrudTest(api.organisation).and(
(org) => new CrudTest(api.mediaAgencyCollections, (col) => col.org = org),
(org) => new CrudTest(api.paymentSubscriptions, (sub) => sub.org = org).and(
(sub) => new CrudTest(api.paymentMethods, (inv) => inv.sub = sub),
(sub) => new CrudTest(api.invoices, (inv) => inv.sub = sub)
),
...Object.values(api.mediaKitCruds).map((crud) =>
(org) => new CrudTest(crud, (kit) => kit.org = org).and(
(kit) => new CrudTest(api.mediaKitObjects.requests, (obj) => obj.kit = kit),
(kit) => new CrudTest(api.mediaKitObjects.adPortals, (obj) => obj.kit = kit),
(kit) => new CrudTest(api.mediaKitObjects.cases, (obj) => obj.kit = kit),
(kit) => new CrudTest(api.mediaKitObjects.products, (obj) => obj.kit = kit),
(kit) => new CrudTest(api.mediaKitObjects.productGroups, (obj) => obj.kit = kit),
(kit) => new CrudTest(api.mediaKitObjects.contacts, (obj) => obj.kit = kit),
(kit) => new CrudTest(api.mediaKitObjects.guidelines, (obj) => obj.kit = kit),
)
)
),
new Test('Logout Organisation Admin', () => state.auth.logout())
)
])
);
function crudUser (withInstance: (instance: User, password: string) => Test[]) {
const password = 'foobar123';
return new CrudTest(User)
.extend({password})
.and((user) => withInstance(user, password));
}
type TestType = 'sequence' | 'parallel';
class Test<Output = any, Input = any> {
private children: Array<(result: Output) => Test<any, Output>> = [];
readonly type: TestType;
constructor (
public name: string,
private operation: (input?: Input) => Promise<FormTransportResponse<Output, any>> = async () => ({})
) {}
async run (input?: Input) {
const result = await this.operation(input);
switch (this.type) {
case 'sequence':
for (const child of this.children) {
await child(result.data).run(result.data);
}
break;
case 'parallel':
await Promise.all(this.children.map((child) => child(result.data).run(result.data)));
break;
}
return result;
}
and (...tests: Array<Test<any, Output> | ((result: Output) => Test<any, Output>)>) {
const fns = tests.map((child) => typeof child === 'function' ? child : () => child);
this.children.push(...fns);
return this;
}
}
class CrudTest<
Model extends {id: Id} = any,
Id = number | string,
TQuery = Query<Model>
> extends Test<Model, Model> {
private extension: object = {};
private all: boolean = false;
constructor (
private crud: Crud<Model, Id, TQuery>,
private mutate: (instance: Model) => any = () => {}
) {
super(`Create ${crud.name}`);
this.and(
new Test<Model, Model>(`Create ${crud.name}`, (instance) => {
const payload = instance || createFixture(getDefaultModelSchema(crud.model));
mutate(payload);
return crud.create(payload, this.extension);
}),
new Test<Model, Model>(`Read ${crud.name}`, (instance) => crud.readOne({id: instance.id} as any)),
this.all && (
new Test<Model[], Model>(`Read all ${crud.name}`, () => crud.read())
),
new Test<Model, Model>(`Update ${crud.name}`, (instance) => crud.update(instance)),
new Test<boolean, Model>(`Delete ${crud.name}`, (instance) => crud.delete(instance))
);
}
extend (params: object) {
Object.assign(this.extension, params);
return this;
}
withAll () {
this.all = true;
return this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment