Skip to content

Commit db0ecec

Browse files
committed
feat(pos-make-findable): Indicate whether the resource is present in any index
1 parent ee8ae9d commit db0ecec

File tree

2 files changed

+67
-20
lines changed

2 files changed

+67
-20
lines changed

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

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ describe('pos-make-findable', () => {
5353
};
5454
when(mockOs.store.get).calledWith('https://thing.example#it').mockReturnValue({ fake: 'thing' });
5555
const labelIndexAssume = jest.fn();
56-
when(labelIndexAssume).calledWith(LabelIndex).mockReturnValue({ fake: 'index' });
56+
when(labelIndexAssume)
57+
.calledWith(LabelIndex)
58+
.mockReturnValue({ fake: 'index', contains: () => false });
5759
when(mockOs.store.get).calledWith('https://pod.example/label-index').mockReturnValue({
5860
assume: labelIndexAssume,
5961
});
@@ -66,10 +68,47 @@ describe('pos-make-findable', () => {
6668
await page.waitForChanges();
6769

6870
// then the thing is added to the index
69-
expect(mockOs.addToLabelIndex).toHaveBeenCalledWith({ fake: 'thing' }, { fake: 'index' });
71+
expect(mockOs.addToLabelIndex).toHaveBeenCalledWith({ fake: 'thing' }, expect.objectContaining({ fake: 'index' }));
7072

71-
// and the state changes to success
72-
expect(page.rootInstance.success).toEqual(true);
73+
// and the state changes to indexed
74+
expect(page.rootInstance.isIndexed).toEqual(true);
75+
});
76+
77+
it('indicates if thing is already indexed', async () => {
78+
// given a user profile in the current session with a single private label index
79+
session.state.isLoggedIn = true;
80+
session.state.profile = {
81+
getPrivateLabelIndexes: () => ['https://pod.example/label-index'],
82+
} as unknown as WebIdProfile;
83+
84+
// and a make findable component for a thing
85+
page = await newSpecPage({
86+
components: [PosMakeFindable],
87+
html: `<pos-make-findable uri="https://thing.example#it"/>`,
88+
});
89+
90+
// and a PodOS instance that yields Thing and LabelIndex instances for the URIs in question
91+
const mockOs = {
92+
store: {
93+
get: jest.fn(),
94+
},
95+
};
96+
when(mockOs.store.get).calledWith('https://thing.example#it').mockReturnValue({ fake: 'thing' });
97+
const contains = jest.fn();
98+
const labelIndexAssume = jest.fn();
99+
when(labelIndexAssume).calledWith(LabelIndex).mockReturnValue({ fake: 'index', contains });
100+
when(mockOs.store.get).calledWith('https://pod.example/label-index').mockReturnValue({
101+
assume: labelIndexAssume,
102+
});
103+
104+
// and the index already contains the URI
105+
when(contains).calledWith('https://thing.example#it').mockReturnValue(true);
106+
107+
// and the component received that PodOs instance
108+
page.rootInstance.receivePodOs(mockOs);
109+
110+
// then the state indicates that the URI is already indexed
111+
expect(page.rootInstance.isIndexed).toEqual(true);
73112
});
74113

75114
it('emits error if updating the index fails', async () => {
@@ -98,7 +137,9 @@ describe('pos-make-findable', () => {
98137
};
99138
when(mockOs.store.get).calledWith('https://thing.example#it').mockReturnValue({ fake: 'thing' });
100139
const labelIndexAssume = jest.fn();
101-
when(labelIndexAssume).calledWith(LabelIndex).mockReturnValue({ fake: 'index' });
140+
when(labelIndexAssume)
141+
.calledWith(LabelIndex)
142+
.mockReturnValue({ fake: 'index', contains: () => false });
102143
when(mockOs.store.get).calledWith('https://pod.example/label-index').mockReturnValue({
103144
assume: labelIndexAssume,
104145
});
@@ -204,7 +245,9 @@ describe('pos-make-findable', () => {
204245
};
205246
when(mockOs.store.get).calledWith('https://thing.example#it').mockReturnValue({ fake: 'thing' });
206247
const labelIndexAssume = jest.fn();
207-
when(labelIndexAssume).calledWith(LabelIndex).mockReturnValue({ fake: 'index' });
248+
when(labelIndexAssume)
249+
.calledWith(LabelIndex)
250+
.mockReturnValue({ fake: 'index', contains: () => false });
208251
when(mockOs.store.get).calledWith('https://pod.example/label-index').mockReturnValue({
209252
assume: labelIndexAssume,
210253
});
@@ -229,7 +272,7 @@ describe('pos-make-findable', () => {
229272

230273
// and is working
231274
fireEvent.click(button);
232-
expect(mockOs.addToLabelIndex).toHaveBeenCalledWith({ fake: 'thing' }, { fake: 'index' });
275+
expect(mockOs.addToLabelIndex).toHaveBeenCalledWith({ fake: 'thing' }, expect.objectContaining({ fake: 'index' }));
233276
});
234277

235278
it('updates thing when URI changes', async () => {
@@ -258,7 +301,7 @@ describe('pos-make-findable', () => {
258301
expect(page.rootInstance.thing).toEqual({ uri: 'https://other.example#it' });
259302
});
260303

261-
it('resets success state when URI changes', async () => {
304+
it('resets index indication when URI changes', async () => {
262305
// given a make findable component for a thing
263306
page = await newSpecPage({
264307
components: [PosMakeFindable],
@@ -274,13 +317,13 @@ describe('pos-make-findable', () => {
274317

275318
// and the component received that PodOs instance already
276319
page.rootInstance.receivePodOs(mockOs);
277-
// and the component is in success state
278-
page.rootInstance.success = true;
320+
// and the component is in indexed state
321+
page.rootInstance.isIndexed = true;
279322

280323
// when the URI attribute changes
281324
page.root.setAttribute('uri', 'https://other.example#it');
282-
// then the success status resets
283-
expect(page.rootInstance.success).toEqual(false);
325+
// then the indexed status resets
326+
expect(page.rootInstance.isIndexed).toEqual(false);
284327
});
285328

286329
describe('multiple indexes to choose from', () => {
@@ -386,8 +429,8 @@ describe('pos-make-findable', () => {
386429
const list = screen.queryByRole('listbox');
387430
expect(list).toBeNull();
388431

389-
// and the state changes to success
390-
expect(page.rootInstance.success).toEqual(true);
432+
// and the state changes to indexed
433+
expect(page.rootInstance.isIndexed).toEqual(true);
391434
});
392435

393436
it('closes the options, if clicked elsewhere', async () => {

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class PosMakeFindable implements PodOsAware {
1919
@State() unsubscribeSessionChange: undefined | (() => void);
2020

2121
@State() showOptions = false;
22-
@State() success = false;
22+
@State() isIndexed = false;
2323

2424
@Event({ eventName: 'pod-os:error' }) errorEmitter: EventEmitter;
2525

@@ -51,7 +51,11 @@ export class PosMakeFindable implements PodOsAware {
5151
@Watch('uri')
5252
updateUri(uri: string) {
5353
this.thing = this.os.store.get(uri);
54-
this.success = false;
54+
this.isIndexed = this.checkIfIndexed(uri);
55+
}
56+
57+
private checkIfIndexed(uri: string) {
58+
return this.indexes.some(it => it.contains(uri));
5559
}
5660

5761
receivePodOs = async (os: PodOS) => {
@@ -66,12 +70,12 @@ export class PosMakeFindable implements PodOsAware {
6670
private getLabelIndexes(profile: WebIdProfile) {
6771
if (profile) {
6872
this.indexes = profile.getPrivateLabelIndexes().map(it => this.os.store.get(it).assume(LabelIndex));
73+
this.isIndexed = this.checkIfIndexed(this.uri);
6974
}
7075
}
7176

7277
private async onClick(e: MouseEvent) {
7378
e.preventDefault();
74-
this.success = false;
7579
if (this.indexes.length === 1) {
7680
await this.addToLabelIndex(this.indexes[0]);
7781
} else if (this.indexes.length > 1) {
@@ -96,7 +100,7 @@ export class PosMakeFindable implements PodOsAware {
96100
private async addToLabelIndex(index: LabelIndex) {
97101
try {
98102
await this.os.addToLabelIndex(this.thing, index);
99-
this.success = true;
103+
this.isIndexed = true;
100104
} catch (e) {
101105
this.errorEmitter.emit(e);
102106
}
@@ -115,11 +119,11 @@ export class PosMakeFindable implements PodOsAware {
115119
<button
116120
type="button"
117121
aria-label={label}
118-
class={{ main: true, open: this.showOptions, success: this.success }}
122+
class={{ main: true, open: this.showOptions, success: this.isIndexed }}
119123
onClick={e => this.onClick(e)}
120124
title=""
121125
>
122-
{this.success ? <IconSuccess /> : <IconMakeFindable />}
126+
{this.isIndexed ? <IconSuccess /> : <IconMakeFindable />}
123127
<p>{label}</p>
124128
</button>
125129
{this.showOptions && (

0 commit comments

Comments
 (0)