Skip to content

Commit

Permalink
[relationships] Relationships are now nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
plusgut committed Jan 21, 2021
1 parent 389234f commit 0a1e51d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 12 deletions.
54 changes: 43 additions & 11 deletions src/components/itemFactory/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ describe("test item", () => {
});

it("commitRelationships, should be nullable", async () => {
const { Repository, Branch, Item } = stateFactory<{
const { Repository, Branch, Item, Merge } = stateFactory<{
blogPost: {
listParameter: {
sort: "asc" | "desc";
Expand Down Expand Up @@ -331,26 +331,51 @@ describe("test item", () => {
},
}}
>
<Branch>
<Merge>
{({ merge, changes }) => (
<Item model="blogPost" id={"1"}>
{(view, { commitRelationships }) =>
view.isLoading ? (
<span>item-loading</span>
) : (
<h1>
<span>
{view.item.relationships.author === null
? "no-author"
: view.item.relationships.author.id}
</span>
<button
onclick={() => {
if (view.item.relationships.author === null) {
merge(changes);
} else {
commitRelationships({
author: null,
});
}
}}
/>
</h1>
)
}
</Item>
)}
</Merge>
</Branch>
<Branch>
<Item model="blogPost" id={"1"}>
{(view, { commitRelationships }) =>
{(view) =>
view.isLoading ? (
<span>item-loading</span>
) : (
<h1>
<h2>
<span>
{view.item.relationships.author === null
? "no-author"
: view.item.relationships.author.id}
</span>
<button
onclick={() =>
commitRelationships({
author: null,
})
}
/>
</h1>
</h2>
)
}
</Item>
Expand All @@ -364,10 +389,17 @@ describe("test item", () => {

expect(wrapper.contains(<span>item-loading</span>)).toBe(false);
expect(wrapper.find("h1").contains(<span>1</span>)).toBe(true);
expect(wrapper.find("h2").contains(<span>1</span>)).toBe(true);

wrapper.find("h1").find("button").simulate("click");

expect(wrapper.find("h1").contains(<span>no-author</span>)).toBe(true);
expect(wrapper.find("h2").contains(<span>1</span>)).toBe(true);

wrapper.find("h1").find("button").simulate("click");

expect(wrapper.find("h1").contains(<span>no-author</span>)).toBe(true);
expect(wrapper.find("h2").contains(<span>no-author</span>)).toBe(true);
});
it("commitAttributes should throw error when item is empty", async () => {
const { Repository, Branch, Item } = stateFactory<{
Expand Down
6 changes: 5 additions & 1 deletion src/components/mergeFactory/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type props<T extends entitiesContainerTemplate> = {
type singleRelationship = entityEmpty<string, string | number>;
type manyRelationships = entityEmpty<string, string | number>[];

type relationships = singleRelationship | manyRelationships;
type relationships = (singleRelationship | null) | manyRelationships;

function isSameSingleRelationship(
a: singleRelationship,
Expand All @@ -56,6 +56,10 @@ function isSameRelationship(a: relationships, b: relationships) {
b.every((value, index) => isSameSingleRelationship(a[index], value))
);
} else if (Array.isArray(a) === false && Array.isArray(b) === false) {
if (a === null || b === null) {
return a === b;
}

return isSameSingleRelationship(
a as singleRelationship,
b as singleRelationship
Expand Down

0 comments on commit 0a1e51d

Please sign in to comment.