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

Error removing relationship with offset path #8

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
60 changes: 43 additions & 17 deletions test/relationship-spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// tslint:disable:no-implicit-dependencies
import { Record, IFmWatchEvent } from "../src";
import { Record, IFmWatchEvent, List, IFmLocalEvent, Watch } from "../src";
import { DB } from "abstracted-admin";
import * as chai from "chai";
const expect = chai.expect;
Expand All @@ -9,6 +9,7 @@ import { FancyPerson } from "./testing/FancyPerson";
import { FmEvents } from "../src/state-mgmt";
import { Company } from "./testing/Company";
import { Pay } from "./testing/Pay";
import { PersonPay } from "./testing/dynamicPaths/PersonPay";
import { extractFksFromPaths } from "../src/record/extractFksFromPaths";
import { Car } from "./testing/Car";
import OffsetCar from "./testing/dynamicPaths/Car";
Expand Down Expand Up @@ -288,20 +289,45 @@ describe("Relationship > ", () => {
);
}
});
// it.skip("using addToRelationship() on a hasMany relationship with an inverse of hasOne", async () => {
// const person = await Record.add(Person, {
// name: "Bob",
// age: 23
// });
// expect(person.id).to.exist.and.to.be.a("string");
// const lastUpdated = person.data.lastUpdated;
// const events: IFmRecordEvent[] = [];
// Record.dispatch = (evt: IFmRecordEvent) => events.push(evt);
// await person.addToRelationship("concerts", "12345");
// expect((person.data.concerts as any)["12345"]).to.equal(true);
// expect(events).to.have.lengthOf(2);
// const eventTypes = new Set(events.map(e => e.type));
// expect(eventTypes.has(FMEvents.RELATIONSHIP_ADDED)).to.equal(true);
// expect(eventTypes.has(FMEvents.RELATIONSHIP_ADDED_LOCALLY)).to.equal(true);
// });

it.only("testing removing relationships with remove()", async () => {
const person = await Record.add(Person, {
id: "p1",
name: "Joe Bloggs",
age: 22,
gender: "male"
});

const events: Array<IFmLocalEvent<Person>> = [];
const cb = async (event: IFmLocalEvent<Person>) => {
events.push(event);
};
const pList = await Watch.list(PersonPay, { person: person.id })
.all()
.dispatch(cb)
.start();

const pay = await Record.add(PersonPay, {
amount: "2400.00",
person: person.id
});

const pay1 = await Record.add(PersonPay, {
amount: "2400.00",
person: person.id
});
console.log(pay.dbPath);
await pay.remove();

expect(events).to.have.lengthOf(10);
const eventTypes = new Set(events.map(e => e.type));
expect(eventTypes.has(FmEvents.RECORD_REMOVED_LOCALLY)).is.equal(true);
expect(eventTypes.has(FmEvents.RECORD_REMOVED_CONFIRMATION)).is.equal(true);

const payList = await List.all(PersonPay, { offsets: { person: person.id }});

expect(payList).to.have.lengthOf(1);
const ids = payList.map(p => p.id);
expect(ids.includes(pay.id)).to.equal(false);
});
}).timeout(4000);
9 changes: 9 additions & 0 deletions test/testing/dynamicPaths/PersonPay.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { model, property, Model, belongsTo, IFmHasOne } from "../../../src";
import { Person } from "../Person";

@model({ dbOffset: ":person", audit: true })
export class PersonPay extends Model {
// prettier-ignore
@belongsTo(() => Person) public person: IFmHasOne;
@property public amount?: string;
}