|
| 1 | +import * as UI from '../../src/index' |
| 2 | +import ContactsModuleRdfLib from '@solid-data-modules/contacts-rdflib' |
| 3 | + |
| 4 | +const CATALOG_URL = |
| 5 | + 'https://raw.githubusercontent.com/solid/catalog/refs/heads/main/catalog-data.ttl' |
| 6 | + |
| 7 | +function makeMockKb () { |
| 8 | + const meWebId = 'https://demo.example/profile/card#me' |
| 9 | + const friendWebId = 'https://friend.example/profile/card#me' |
| 10 | + const personFromKnowsWebId = 'https://person.example/profile/card#me' |
| 11 | + const contactCardUri = 'https://demo.example/contacts/alice#this' |
| 12 | + |
| 13 | + const namesByWebId = { |
| 14 | + [friendWebId]: 'Frank Friend', |
| 15 | + [personFromKnowsWebId]: 'Pat Person' |
| 16 | + } |
| 17 | + |
| 18 | + return { |
| 19 | + fetcher: { |
| 20 | + load: async function () { |
| 21 | + return undefined |
| 22 | + } |
| 23 | + }, |
| 24 | + updater: {}, |
| 25 | + any: function (subject, predicate) { |
| 26 | + const subjectValue = subject && subject.value |
| 27 | + const predicateValue = predicate && predicate.value |
| 28 | + |
| 29 | + if (!subjectValue || !predicateValue) { |
| 30 | + return null |
| 31 | + } |
| 32 | + |
| 33 | + if ( |
| 34 | + predicateValue.includes('foaf/0.1/name') || |
| 35 | + predicateValue.endsWith('#name') || |
| 36 | + predicateValue.includes('/2006/vcard/ns#fn') |
| 37 | + ) { |
| 38 | + const personName = namesByWebId[subjectValue] |
| 39 | + return personName ? { value: personName } : null |
| 40 | + } |
| 41 | + |
| 42 | + if ( |
| 43 | + predicateValue.includes('/2006/vcard/ns#url') && |
| 44 | + subjectValue === contactCardUri |
| 45 | + ) { |
| 46 | + return $rdf.namedNode(`${contactCardUri}-url`) |
| 47 | + } |
| 48 | + |
| 49 | + return null |
| 50 | + }, |
| 51 | + anyValue: function (subject, predicate) { |
| 52 | + const subjectValue = subject && subject.value |
| 53 | + const predicateValue = predicate && predicate.value |
| 54 | + |
| 55 | + if (!subjectValue || !predicateValue) { |
| 56 | + return null |
| 57 | + } |
| 58 | + |
| 59 | + if ( |
| 60 | + predicateValue.includes('/2006/vcard/ns#value') && |
| 61 | + subjectValue === `${contactCardUri}-url` |
| 62 | + ) { |
| 63 | + return 'https://alice.example/profile/card#me' |
| 64 | + } |
| 65 | + |
| 66 | + return null |
| 67 | + }, |
| 68 | + each: function (subject, predicate) { |
| 69 | + const subjectValue = subject && subject.value |
| 70 | + const predicateValue = predicate && predicate.value |
| 71 | + |
| 72 | + if (!subjectValue || !predicateValue) { |
| 73 | + return [] |
| 74 | + } |
| 75 | + |
| 76 | + if (!predicateValue.includes('foaf/0.1/knows')) { |
| 77 | + return [] |
| 78 | + } |
| 79 | + |
| 80 | + if (subjectValue === meWebId) { |
| 81 | + return [ |
| 82 | + $rdf.namedNode(friendWebId), |
| 83 | + $rdf.namedNode(personFromKnowsWebId) |
| 84 | + ] |
| 85 | + } |
| 86 | + |
| 87 | + return [] |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +function installPeopleSearchMocks () { |
| 93 | + const originalListAddressBooks = |
| 94 | + ContactsModuleRdfLib.prototype.listAddressBooks |
| 95 | + const originalReadAddressBook = |
| 96 | + ContactsModuleRdfLib.prototype.readAddressBook |
| 97 | + const originalFetch = globalThis.fetch |
| 98 | + |
| 99 | + ContactsModuleRdfLib.prototype.listAddressBooks = async function () { |
| 100 | + return { |
| 101 | + publicUris: ['https://demo.example/address-book#this'], |
| 102 | + privateUris: [] |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + ContactsModuleRdfLib.prototype.readAddressBook = async function () { |
| 107 | + return { |
| 108 | + contacts: [ |
| 109 | + { |
| 110 | + uri: 'https://demo.example/contacts/alice#this', |
| 111 | + name: 'Alice Contact' |
| 112 | + } |
| 113 | + ] |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + globalThis.fetch = async function (input, init) { |
| 118 | + const url = typeof input === 'string' ? input : input && input.url |
| 119 | + |
| 120 | + if (url === CATALOG_URL) { |
| 121 | + return { |
| 122 | + ok: true, |
| 123 | + status: 200, |
| 124 | + text: async function () { |
| 125 | + return ` |
| 126 | + @prefix ex: <http://example.org#> . |
| 127 | +
|
| 128 | + <https://catalog-person.example/profile/card#me> a ex:Person ; |
| 129 | + ex:name "Catalog Person" ; |
| 130 | + ex:webid <https://catalog-person.example/profile/card#me> . |
| 131 | + ` |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + if (typeof originalFetch === 'function') { |
| 137 | + return originalFetch(input, init) |
| 138 | + } |
| 139 | + |
| 140 | + return { |
| 141 | + ok: false, |
| 142 | + status: 404, |
| 143 | + text: async function () { |
| 144 | + return '' |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + return function restoreMocks () { |
| 150 | + ContactsModuleRdfLib.prototype.listAddressBooks = originalListAddressBooks |
| 151 | + ContactsModuleRdfLib.prototype.readAddressBook = originalReadAddressBook |
| 152 | + globalThis.fetch = originalFetch |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +export default { |
| 157 | + title: 'Widgets/PeopleSearch' |
| 158 | +} |
| 159 | + |
| 160 | +export const SignedOut = { |
| 161 | + render: () => { |
| 162 | + return UI.widgets.createPeopleSearch(document, makeMockKb(), null) |
| 163 | + }, |
| 164 | + name: 'signed out' |
| 165 | +} |
| 166 | + |
| 167 | +export const WithMockData = { |
| 168 | + render: () => { |
| 169 | + const restoreMocks = installPeopleSearchMocks() |
| 170 | + |
| 171 | + const wrapper = document.createElement('div') |
| 172 | + const info = document.createElement('p') |
| 173 | + info.textContent = |
| 174 | + 'Mocked sources: address book, foaf:knows, and Solid catalog. Type to filter.' |
| 175 | + wrapper.appendChild(info) |
| 176 | + |
| 177 | + const me = $rdf.namedNode('https://demo.example/profile/card#me') |
| 178 | + const picker = UI.widgets.createPeopleSearch(document, makeMockKb(), me) |
| 179 | + wrapper.appendChild(picker) |
| 180 | + |
| 181 | + const cleanup = function () { |
| 182 | + restoreMocks() |
| 183 | + wrapper.removeEventListener('DOMNodeRemovedFromDocument', cleanup) |
| 184 | + } |
| 185 | + wrapper.addEventListener('DOMNodeRemovedFromDocument', cleanup) |
| 186 | + |
| 187 | + return wrapper |
| 188 | + }, |
| 189 | + name: 'with mock data' |
| 190 | +} |
0 commit comments