Skip to content

Commit 735eb6a

Browse files
committed
feat: emit error when adding to index fails
1 parent b8a1efd commit 735eb6a

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

elements/src/components/pos-make-findable/pos-make-findable.spec.tsx

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,52 @@ describe('pos-make-findable', () => {
6868
expect(mockOs.addToLabelIndex).toHaveBeenCalledWith({ fake: 'thing' }, { fake: 'index' });
6969
});
7070

71+
fit('emits error if updating the index fails', async () => {
72+
// given a user profile in the current session with a single private label index
73+
session.state.isLoggedIn = true;
74+
session.state.profile = {
75+
getPrivateLabelIndexes: () => ['https://pod.example/label-index'],
76+
} as unknown as WebIdProfile;
77+
78+
// and a make findable component for a thing
79+
page = await newSpecPage({
80+
components: [PosMakeFindable],
81+
html: `<pos-make-findable uri="https://thing.example#it"/>`,
82+
});
83+
84+
// and the page listens for error
85+
const errorListener = jest.fn();
86+
page.root.addEventListener('pod-os:error', errorListener);
87+
88+
// and a PodOS instance that yields Thing and LabelIndex instances for the URIs in question
89+
const mockOs = {
90+
store: {
91+
get: jest.fn(),
92+
},
93+
addToLabelIndex: jest.fn(),
94+
};
95+
when(mockOs.store.get).calledWith('https://thing.example#it').mockReturnValue({ fake: 'thing' });
96+
const labelIndexAssume = jest.fn();
97+
when(labelIndexAssume).calledWith(LabelIndex).mockReturnValue({ fake: 'index' });
98+
when(mockOs.store.get).calledWith('https://pod.example/label-index').mockReturnValue({
99+
assume: labelIndexAssume,
100+
});
101+
102+
// but leads to an error when adding a thing to index
103+
mockOs.addToLabelIndex.mockRejectedValue(new Error('simulated error'));
104+
105+
// and the component received that PodOs instance
106+
page.rootInstance.receivePodOs(mockOs);
107+
108+
// when the button is clicked
109+
const button = screen.getByRole('button');
110+
fireEvent.click(button);
111+
await page.waitForChanges();
112+
113+
// then an error event is emitted
114+
expect(errorListener).toHaveBeenCalledWith(expect.objectContaining({ detail: new Error('simulated error') }));
115+
});
116+
71117
it('does not show up, when not logged in', async () => {
72118
// given no user session
73119
session.state.isLoggedIn = false;

elements/src/components/pos-make-findable/pos-make-findable.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, Element, Event, h, Host, Listen, Prop, State, Watch } from '@stencil/core';
1+
import { Component, Element, Event, EventEmitter, h, Host, Listen, Prop, State, Watch } from '@stencil/core';
22
import { LabelIndex, PodOS, Thing, WebIdProfile } from '@pod-os/core';
33
import { PodOsAware, PodOsEventEmitter, subscribePodOs } from '../events/PodOsAware';
44
import session from '../../store/session';
@@ -20,6 +20,8 @@ export class PosMakeFindable implements PodOsAware {
2020

2121
@State() showOptions = false;
2222

23+
@Event({ eventName: 'pod-os:error' }) errorEmitter: EventEmitter;
24+
2325
@Element() el: HTMLElement;
2426

2527
componentWillLoad() {
@@ -75,7 +77,12 @@ export class PosMakeFindable implements PodOsAware {
7577
}
7678

7779
private async addToLabelIndex(index: LabelIndex) {
78-
await this.os.addToLabelIndex(this.thing, index);
80+
try {
81+
await this.os.addToLabelIndex(this.thing, index);
82+
} catch (e) {
83+
this.errorEmitter.emit(e);
84+
}
85+
7986
this.showOptions = false;
8087
}
8188

0 commit comments

Comments
 (0)