Skip to content

Commit 7efe5bd

Browse files
committed
feat(pos-add-relation): fire error event and keep inputs when save failed
1 parent 7262629 commit 7efe5bd

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

elements/src/components/pos-add-relation/pos-add-relation.spec.tsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,41 @@ describe('pos-add-relation', () => {
146146
}),
147147
);
148148
});
149+
150+
it('fires error event and keeps inputs when save failed', async () => {
151+
// given the resource in context is editable
152+
const thing = { editable: true, fake: 'thing' };
153+
mockResource(thing);
154+
155+
// and a page with a pos-add-relation component
156+
const page = await newSpecPage({
157+
supportsShadowDom: false,
158+
components: [PosAddRelation],
159+
html: `<pos-add-relation></pos-add-relation>`,
160+
});
161+
162+
// and the page listens for pod-os:error event
163+
const eventListener = jest.fn();
164+
page.root.addEventListener('pod-os:error', eventListener);
165+
166+
// and saving will cause an error
167+
const error = new Error('fake error in addPropertyValue');
168+
when(os.addRelation).mockRejectedValue(error);
169+
170+
// when save is called
171+
page.rootInstance.selectedTermUri = 'http://xmlns.com/foaf/0.1/knows';
172+
page.rootInstance.currentValue = 'https://alice.test/profile/card#me';
173+
await page.rootInstance.save();
174+
175+
// then a pod-os:error event with the error is received in the listener
176+
expect(eventListener).toHaveBeenCalledWith(
177+
expect.objectContaining({
178+
detail: error,
179+
}),
180+
);
181+
182+
// and the value input is not cleared
183+
expect(page.rootInstance.selectedTermUri).toBe('http://xmlns.com/foaf/0.1/knows');
184+
expect(page.rootInstance.currentValue).toBe('https://alice.test/profile/card#me');
185+
});
149186
});

elements/src/components/pos-add-relation/pos-add-relation.tsx

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ export class PosAddRelation {
3333
*/
3434
@Event({ eventName: 'pod-os:added-relation' }) addedRelation: EventEmitter<Relation>;
3535

36+
/**
37+
* Something went wrong while adding the relation.
38+
*/
39+
@Event({ eventName: 'pod-os:error' }) error: EventEmitter;
40+
3641
private valueInput: HTMLInputElement;
3742

3843
async componentWillLoad() {
@@ -46,14 +51,18 @@ export class PosAddRelation {
4651
}
4752

4853
async save() {
49-
await this.os.addRelation(this.resource, this.selectedTermUri, this.currentValue);
50-
const relation = {
51-
predicate: this.selectedTermUri,
52-
label: labelFromUri(this.selectedTermUri),
53-
uris: [this.currentValue],
54-
};
54+
try {
55+
await this.os.addRelation(this.resource, this.selectedTermUri, this.currentValue);
56+
const relation = {
57+
predicate: this.selectedTermUri,
58+
label: labelFromUri(this.selectedTermUri),
59+
uris: [this.currentValue],
60+
};
5561
this.addedRelation.emit(relation);
56-
this.currentValue = '';
62+
this.currentValue = '';
63+
} catch (e) {
64+
this.error.emit(e);
65+
}
5766
}
5867

5968
render() {

0 commit comments

Comments
 (0)