Skip to content

Commit e64f73d

Browse files
jg10-mastodon-socialangelo-v
authored andcommitted
feat(pos-list): support if-typeof attribute
1 parent d310dab commit e64f73d

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

elements/src/components/pos-list/pos-list.spec.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { newSpecPage } from '@stencil/core/testing';
22
import { PosList } from './pos-list';
3+
import { when } from 'jest-when';
34

45
describe('pos-list', () => {
56
it('contains only template initially', async () => {
@@ -70,6 +71,29 @@ describe('pos-list', () => {
7071
expect(el.querySelectorAll('pos-resource')).toHaveLength(2);
7172
});
7273

74+
it('renders if-typeof objects', async () => {
75+
const page = await newSpecPage({
76+
components: [PosList],
77+
html: `
78+
<pos-list if-typeof="http://schema.org/Recipe">
79+
<template>
80+
Test
81+
</template>
82+
</pos-list>`,
83+
});
84+
const os = {
85+
store: {
86+
findMembers: jest.fn(),
87+
},
88+
};
89+
when(os.store.findMembers).calledWith('http://schema.org/Recipe').mockReturnValue(['https://recipe.test/1']);
90+
await page.rootInstance.receivePodOs(os);
91+
await page.waitForChanges();
92+
93+
const el: HTMLElement = page.root as unknown as HTMLElement;
94+
expect(el.querySelector('pos-resource')?.getAttribute('about')).toEqual('https://recipe.test/1');
95+
});
96+
7397
it('displays error on missing template', async () => {
7498
const page = await newSpecPage({
7599
components: [PosList],

elements/src/components/pos-list/pos-list.tsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,41 @@
1-
import { Thing } from '@pod-os/core';
1+
import { PodOS, Thing } from '@pod-os/core';
22
import { Component, Element, Event, h, Prop, State } from '@stencil/core';
33
import { ResourceAware, ResourceEventEmitter, subscribeResource } from '../events/ResourceAware';
4+
import { PodOsAware, PodOsEventEmitter, subscribePodOs } from '../events/PodOsAware';
45

56
@Component({
67
tag: 'pos-list',
78
shadow: false,
89
})
9-
export class PosList implements ResourceAware {
10+
export class PosList implements PodOsAware, ResourceAware {
1011
/**
1112
* URI of the predicate to follow
1213
*/
1314
@Prop() rel: string;
15+
/**
16+
* URI of a class for which instances will be listed
17+
*/
18+
@Prop() ifTypeof: string;
1419
/**
1520
* Whether listed resources should be fetched before being displayed
1621
*/
1722
@Prop() fetch: boolean = false;
1823

1924
@Element() host: HTMLElement;
2025
@State() error: string = null;
26+
@State() os: PodOS;
2127
@State() resource: Thing;
2228
@State() items: string[] = [];
2329
@State() templateString: string;
2430

2531
@Event({ eventName: 'pod-os:resource' })
2632
subscribeResource: ResourceEventEmitter;
33+
@Event({ eventName: 'pod-os:init' })
34+
subscribePodOs: PodOsEventEmitter;
2735

2836
componentWillLoad() {
29-
subscribeResource(this);
37+
if (this.rel) subscribeResource(this);
38+
if (this.ifTypeof) subscribePodOs(this);
3039
const templateElement = this.host.querySelector('template');
3140
if (templateElement == null) {
3241
this.error = 'No template element found';
@@ -36,10 +45,14 @@ export class PosList implements ResourceAware {
3645
}
3746

3847
receiveResource = (resource: Thing) => {
39-
this.items = [];
4048
if (this.rel) this.items = resource.relations(this.rel).flatMap(relation => relation.uris);
4149
};
4250

51+
receivePodOs = async (os: PodOS) => {
52+
this.os = os;
53+
this.items = os.store.findMembers(this.ifTypeof);
54+
};
55+
4356
render() {
4457
if (this.error) return this.error;
4558
const elems = this.items.map(it => (

0 commit comments

Comments
 (0)