Skip to content

Commit

Permalink
fix: add showLinkIframe and showLinkButton in settings with tests (#878)
Browse files Browse the repository at this point in the history
* fix: add showLinkIframe and showLinkButton in settings with tests

* refactor: fix tests
  • Loading branch information
pyphilia authored Mar 13, 2024
1 parent e81a7b5 commit 5663bf1
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/services/item/fluent-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ const settings = S.object()
.prop('displayCoEditors', S.boolean())
.prop('descriptionPlacement', S.enum(Object.values(DescriptionPlacement)))
.prop('isCollapsible', S.boolean())
.prop('enableSaveActions', S.boolean());
.prop('enableSaveActions', S.boolean())
// link settings
.prop('showLinkIframe', S.boolean())
.prop('showLinkButton', S.boolean());

export const partialMember = S.object()
.additionalProperties(false)
Expand Down
4 changes: 4 additions & 0 deletions src/services/item/plugins/embeddedLink/embeddedLink.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ describe('Link Item tests', () => {
},
},
description: iframelyMeta.description,
settings: {
showLinkIframe: false,
showLinkButton: true,
},
};

// check response value
Expand Down
7 changes: 7 additions & 0 deletions src/services/item/plugins/embeddedLink/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ const plugin: FastifyPluginAsync<GraaspEmbeddedLinkItemOptions> = async (fastify
embeddedLink.icons = links
.filter(({ rel }: { rel: string[] }) => hasIconRel(rel))
.map(({ href }) => href);

// default settings
item.settings = {
showLinkButton: true,
showLinkIframe: false,
...(item.settings ?? {}),
};
};

itemService.hooks.setPreHook('create', hook);
Expand Down
3 changes: 2 additions & 1 deletion src/services/item/plugins/file/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ class FileItemService {
}
}

return newItem;
// retrieve item again since hasThumbnail might have changed
return repositories.itemRepository.get(newItem.id);
});
}

Expand Down
6 changes: 6 additions & 0 deletions src/services/item/test/fixtures/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ export const expectItem = (
if (newItem.creator && creator) {
expect(newItem.creator.id).toEqual(creator.id);
}

if (correctItem.settings) {
for (const [k, s] of Object.entries(correctItem.settings)) {
expect(newItem.settings![k]).toEqual(s);
}
}
};

export const expectManyItems = (
Expand Down
32 changes: 31 additions & 1 deletion src/services/item/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ describe('Item routes tests', () => {
});

const newItem = response.json();
expectItem(newItem, payload, actor);
expectItem(newItem, { ...payload, settings: VALID_SETTING }, actor);
expect(response.statusCode).toBe(StatusCodes.OK);
expect(newItem.settings.descriptionPlacement).toBe(VALID_SETTING.descriptionPlacement);
expect(Object.keys(newItem.settings)).not.toContain(Object.keys(BAD_SETTING)[0]);
Expand Down Expand Up @@ -1997,6 +1997,35 @@ describe('Item routes tests', () => {
expect(newItem.settings.hasThumbnail).toBeFalsy();
});

it('Update successfully link settings', async () => {
const { item } = await saveItemAndMembership({
member: actor,
});
const payload = {
settings: {
showLinkButton: false,
showLinkIframe: true,
},
};

const response = await app.inject({
method: HttpMethod.Patch,
url: `/items/${item.id}`,
payload,
});

const newItem = response.json();

expectItem(newItem, {
...item,
...payload,
});
expect(response.statusCode).toBe(StatusCodes.OK);
expect(newItem.settings.showLinkButton).toBe(false);
expect(newItem.settings.showLinkIframe).toBe(true);
expect(newItem.settings.hasThumbnail).toBeFalsy();
});

it('Filter out bad setting when updating', async () => {
const BAD_SETTING = { INVALID: 'Not a valid setting' };
const VALID_SETTING = { descriptionPlacement: DescriptionPlacement.ABOVE };
Expand Down Expand Up @@ -2024,6 +2053,7 @@ describe('Item routes tests', () => {
expectItem(newItem, {
...item,
...payload,
settings: VALID_SETTING,
});
expect(response.statusCode).toBe(StatusCodes.OK);
expect(newItem.settings.descriptionPlacement).toBe(VALID_SETTING.descriptionPlacement);
Expand Down

0 comments on commit 5663bf1

Please sign in to comment.