Skip to content

Commit 7262629

Browse files
committed
feat(pos-add-relation): fire event after successful save
1 parent 4f264a7 commit 7262629

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

elements/src/components/pos-add-literal-value/pos-add-literal-value.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class PosAddLiteralValue implements ResourceAware, PodOsAware {
2424
/**
2525
* The entered literal value has been added to the resource and successfully stored to the Pod.
2626
*/
27-
@Event({ eventName: 'pod-os:added-literal-value' }) addedLiteralValue: EventEmitter;
27+
@Event({ eventName: 'pod-os:added-literal-value' }) addedLiteralValue: EventEmitter<Literal>;
2828

2929
/**
3030
* Something went wrong while adding the literal value.

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

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
jest.mock('../events/useResource');
2+
jest.mock('@pod-os/core', () => ({
3+
labelFromUri: uri => `fake label for ${uri}`,
4+
}));
25
import { mockPodOS } from '../../test/mockPodOS';
3-
46
import { newSpecPage } from '@stencil/core/testing';
57
import { PosAddRelation } from './pos-add-relation';
68
import { useResource } from '../events/useResource';
79
import { when } from 'jest-when';
810

9-
import { PodOS, Thing } from '@pod-os/core';
11+
import { PodOS, Relation, Thing } from '@pod-os/core';
1012
import { fireEvent } from '@testing-library/dom';
1113

1214
function mockResource(thing: Partial<Thing> = {}) {
@@ -110,4 +112,38 @@ describe('pos-add-relation', () => {
110112
expect(input.value).toEqual('');
111113
expect(page.rootInstance.currentValue).toBe('');
112114
});
115+
116+
it('fires event after save', async () => {
117+
// given the resource in context is editable
118+
const thing = { editable: true, fake: 'thing' };
119+
mockResource(thing);
120+
121+
// and a page with a pos-add-relation component
122+
const page = await newSpecPage({
123+
supportsShadowDom: false,
124+
components: [PosAddRelation],
125+
html: `<pos-add-relation></pos-add-relation>`,
126+
});
127+
128+
// and the page listens for pod-os:added-relation events
129+
const eventListener = jest.fn();
130+
page.root.addEventListener('pod-os:added-relation', eventListener);
131+
132+
// when save is called
133+
page.rootInstance.selectedTermUri = 'http://xmlns.com/foaf/0.1/knows';
134+
page.rootInstance.currentValue = 'https://alice.test/profile/card#me';
135+
await page.rootInstance.save();
136+
137+
// then a pod-os:added-relation event with the added relation is received in the listener
138+
const relation: Relation = {
139+
predicate: 'http://xmlns.com/foaf/0.1/knows',
140+
label: 'fake label for http://xmlns.com/foaf/0.1/knows',
141+
uris: ['https://alice.test/profile/card#me'],
142+
};
143+
expect(eventListener).toHaveBeenCalledWith(
144+
expect.objectContaining({
145+
detail: relation,
146+
}),
147+
);
148+
});
113149
});

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Component, h, Host, Element, State } from '@stencil/core';
1+
import { Component, h, Host, Element, State, Event, EventEmitter } from '@stencil/core';
22
import { useResource } from '../events/useResource';
33

44
import '@shoelace-style/shoelace/dist/components/icon/icon.js';
5-
import { PodOS, Thing } from '@pod-os/core';
5+
import { labelFromUri, PodOS, Relation, Thing } from '@pod-os/core';
66
import { usePodOS } from '../events/usePodOS';
77

88
/**
@@ -28,6 +28,11 @@ export class PosAddRelation {
2828

2929
@State() currentValue: string;
3030

31+
/**
32+
* The relation has been added to the resource and successfully stored to the Pod.
33+
*/
34+
@Event({ eventName: 'pod-os:added-relation' }) addedRelation: EventEmitter<Relation>;
35+
3136
private valueInput: HTMLInputElement;
3237

3338
async componentWillLoad() {
@@ -42,6 +47,12 @@ export class PosAddRelation {
4247

4348
async save() {
4449
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+
};
55+
this.addedRelation.emit(relation);
4556
this.currentValue = '';
4657
}
4758

@@ -55,11 +66,11 @@ export class PosAddRelation {
5566
<pos-select-term placeholder="Add relation" onPod-os:term-selected={ev => this.onTermSelected(ev)} />
5667
<input
5768
ref={el => (this.valueInput = el)}
58-
value={this.currentValue}
59-
onInput={ev => (this.currentValue = (ev.target as HTMLInputElement).value)}
6069
type="url"
6170
aria-label="URI"
6271
placeholder=""
72+
value={this.currentValue}
73+
onInput={ev => (this.currentValue = (ev.target as HTMLInputElement).value)}
6374
onChange={() => this.save()}
6475
></input>
6576
</Host>

0 commit comments

Comments
 (0)