Skip to content

Commit

Permalink
added new test case
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshBot-Debug committed Feb 24, 2024
1 parent 563652b commit 446ddb6
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jjmyers/object-relationship-store",
"version": "3.0.7",
"version": "3.0.8",
"description": "A javascript object relationship store.",
"main": "build/index.js",
"module": "build/index.esm.js",
Expand Down
58 changes: 58 additions & 0 deletions src/__tests__/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1669,3 +1669,61 @@ test("#A, #B ref #C - #A is deleted", () => {
c: { "3": ["b.2.c"] },
});
});

test("#A1, #A2 ref #B - #A1 is deleted", () => {
const a = createRelationalObject("a");
const b = createRelationalObject("b");

a.hasOne(b);

const store = createStore({
relationalCreators: [a, b],
identifier: {
a: (o) => "isA" in o,
b: (o) => "isB" in o,
},
});

const _b = {
id: 3,
isB: true,
};

const _a1 = {
id: 1,
b: _b,
isA: true,
};

const _a2 = {
id: 2,
b: _b,
isA: true,
};

store.mutate([_a1, _a2]);

expect(store.getState()).toStrictEqual({
a: { "1": { id: 1, isA: true, b: 3 }, "2": { id: 2, isA: true, b: 3 } },
b: { "3": { id: 3, isB: true } },
});

expect(store.getReferences()).toStrictEqual({
b: { "3": ["a.1.b", "a.2.b"] },
});

store.mutate({
id: 1,
isA: true,
__destroy__: true,
});

expect(store.getState()).toStrictEqual({
a: { "2": { id: 2, isA: true, b: 3 } },
b: { "3": { id: 3, isB: true } },
});

expect(store.getReferences()).toStrictEqual({
b: { "3": ["a.2.b"] },
});
});
5 changes: 4 additions & 1 deletion src/lib/createStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export function createStore<
*/
Object.entries(itemSchema.__relationship).forEach(
([field, relationship]) => {

if (!state[name][item[primaryKey]]) return;

const itemPrimaryKey = state[name][item[primaryKey]][field];
Expand Down Expand Up @@ -156,9 +157,11 @@ export function createStore<
});

if (!allSelfRef) {

for (let i = 0; i < refs.length; i++) {
const [refName, refPrimaryKey, refField] = refs[i].split(".");
if (refName === name) {

if (refName === name && item[primaryKey] == refPrimaryKey) {
cleanReferences(
`${refName}.${refPrimaryKey}.${refField}`,
itemPrimaryKey
Expand Down

0 comments on commit 446ddb6

Please sign in to comment.