Skip to content

Commit

Permalink
fix: initiative fromEditor creates new initiatives before attempting … (
Browse files Browse the repository at this point in the history
  • Loading branch information
benstoltz authored May 14, 2024
1 parent 7731f31 commit bbef6ab
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
21 changes: 16 additions & 5 deletions packages/common/src/initiatives/HubInitiative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,20 @@ export class HubInitiative
const autoShareGroups = editor._groups || [];

// 2. convert the editor values back to a initiative entity
const entity = await editorToInitiative(editor, this.context);
let entity = await editorToInitiative(editor, this.context);

// 3. If the entity hasn't been created then we need to do that before we can
// create a featured image, if one has been provided.
if (!entity.id && featuredImage) {
// update this.entity so that the save method will work
this.entity = entity;
// save the entity to get an id / create it
await this.save();
// update the local entity let so that the featured image call can pick up the id
entity = this.entity;
}

// 3. set the thumbnailCache to ensure that
// 4. set the thumbnailCache to ensure that
// the thumbnail is updated on the next save
if (thumbnail) {
if (thumbnail.blob) {
Expand All @@ -344,7 +355,7 @@ export class HubInitiative
}
}

// 4. upsert or remove the configured featured image
// 5. upsert or remove the configured featured image
if (featuredImage) {
let featuredImageUrl: string | null = null;
if (featuredImage.blob) {
Expand Down Expand Up @@ -376,11 +387,11 @@ export class HubInitiative
};
}

// 5. save or create the entity
// 6. save or create the entity
this.entity = entity;
await this.save();

// 6. share the entity with the configured groups
// 7. share the entity with the configured groups
const isCreate = !editor.id;
if (isCreate) {
await this.setAccess(editor.access as SettableAccessLevel);
Expand Down
35 changes: 35 additions & 0 deletions packages/common/test/initiatives/HubInitiative.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,41 @@ describe("HubInitiative Class:", () => {
expect(createSpy).not.toHaveBeenCalled();
expect(upsertResourceSpy).toHaveBeenCalledTimes(1);
});
it("handles setting featured image on creation", async () => {
const chk = HubInitiative.fromJson(
{
name: "Test Entity",
},
authdCtxMgr.context
);
const editor = await chk.toEditor();
editor.view = {
featuredImage: {
blob: "some blob",
filename: "some-featuredImage.png",
},
};
const upsertResourceSpy = spyOn(
upsertResourceModule,
"upsertResource"
).and.returnValue(
Promise.resolve("https://blah.com/some-featuredImage.png")
);

editor.access = "org";

const accessSpy = spyOn(
HubItemEntity.prototype,
"setAccess"
).and.returnValue(Promise.resolve());

await chk.fromEditor(editor);
expect(createSpy).toHaveBeenCalledTimes(1);
expect(updateSpy).toHaveBeenCalledTimes(1);
expect(upsertResourceSpy).toHaveBeenCalledTimes(1);
expect(accessSpy).toHaveBeenCalledTimes(1);
expect(accessSpy).toHaveBeenCalledWith("org");
});
it("handles setting featured image and clearing prior image", async () => {
const chk = HubInitiative.fromJson(
{
Expand Down

0 comments on commit bbef6ab

Please sign in to comment.