Skip to content

Commit 81dc9f8

Browse files
timbltimea-solid
authored andcommitted
Add first test for language module
1 parent 0b649b3 commit 81dc9f8

2 files changed

Lines changed: 92 additions & 3 deletions

File tree

src/widgets/forms/autocomplete/language.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,17 @@ export const languageCodeURIBase = 'https://www.w3.org/ns/iana/language-code/' /
2727
export const defaultPreferedLangages = ['en', 'fr', 'de', 'it', 'ar']
2828

2929
export async function getPreferredLanagugesFor (person: NamedNode) {
30-
await kb.fetcher.load(person.doc())
31-
const list = kb.any(person, ns.schema('knowsLanguage'), null, person.doc()) as Collection | undefined
30+
const doc = person.doc()
31+
await kb.fetcher.load(doc)
32+
const list = kb.any(person, ns.schema('knowsLanguage'), null, doc) as Collection | undefined
3233
if (!list) {
3334
console.log(`User ${person} has not set their languages in their profile.`)
3435
return null // differnet from []
3536
}
3637
const languageCodeArray: string[] = []
3738
list.elements.forEach(item => {
38-
const lang = kb.any(item as any, ns.solid('publicId'), null, (item as NamedNode).doc())
39+
// console.log('@@ item ' + item)
40+
const lang = kb.any(item as any, ns.solid('publicId'), null, doc)
3941
if (!lang) {
4042
console.warn('getPreferredLanguages: No publiID of language.')
4143
return
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/* Test the language handling
2+
*/
3+
4+
import { getFileContent } from '../../../helpers/getFileContent'
5+
// Note different helpers directory:
6+
// import { silenceDebugMessages } from '../../../../helpers/setup'
7+
import { parse } from 'rdflib'
8+
import {
9+
autocompleteField
10+
} from '../../../../../src/widgets/forms/autocomplete/autocompleteField'
11+
import {
12+
languageCodeURIBase,
13+
defaultPreferedLangages,
14+
getPreferredLanagugesFor,
15+
getPreferredLanguages,
16+
filterByLanguage
17+
} from '../../../../../src/widgets/forms/autocomplete/language'
18+
import { store, ns } from '../../../../../src/'
19+
// import { textInputStyle } from '../../../../../src/style'
20+
import {
21+
// findByAltText,
22+
findByTestId,
23+
// getByAltText,
24+
// queryByAltText,
25+
waitFor
26+
} from '@testing-library/dom'
27+
import nock from 'nock'
28+
29+
// jest.unmock('rdflib') // we need Fetcher to work (mocked)
30+
jest.unmock('debug') // while debugging only @@
31+
jest.mock('solid-auth-client', () => ({
32+
currentSession: () => Promise.resolve(),
33+
trackSession: () => null
34+
}))
35+
36+
const prefixes = `@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
37+
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
38+
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
39+
@prefix lang: <${languageCodeURIBase}>.
40+
@prefix owl: <http://www.w3.org/2002/07/owl#>.
41+
@prefix solid: <http://www.w3.org/ns/solid/terms#>.
42+
@prefix ui: <http://www.w3.org/ns/ui#>.
43+
@prefix schema: <http://schema.org/>.
44+
@prefix vcard: <http://www.w3.org/2006/vcard/ns#>.
45+
46+
@prefix org: <http://www.w3.org/ns/org#>.
47+
@prefix esco: <http://data.europa.eu/esco/model#>.
48+
@prefix wd: <http://www.wikidata.org/entity/>.
49+
@prefix wdt: <http://www.wikidata.org/prop/direct/>.
50+
51+
@prefix : <#>.
52+
`
53+
const alice = store.sym('https://alice.example.com/profile#me')
54+
const aliceProfileText = prefixes + `
55+
<#me> a foaf:Person; schema:knowsLanguage ( [ solid:publicId lang:el ] ) . # Just greek
56+
`
57+
parse(aliceProfileText, store, alice.doc().uri)
58+
59+
const bob = store.sym('https://bob.example.com/profile#me')
60+
const bobProfileText = prefixes + `
61+
<#me> a foaf:Person; schema:knowsLanguage ( [ solid:publicId lang:en] [ solid:publicId lang:fr] ) . # Just english and french
62+
`
63+
/*
64+
const charlieProfileText = prefixes + `
65+
<#me> a foaf:Person . # Nothing stated about languages
66+
`
67+
*/
68+
const kb = store
69+
70+
describe('getPreferredLanagugesFor', () => {
71+
let result
72+
beforeEach(() => {
73+
// fetch.resetMocks();
74+
})
75+
76+
it('exists as a function', () => {
77+
expect(getPreferredLanagugesFor).toBeInstanceOf(Function)
78+
})
79+
it('returns just greek for Alice, plus deafults', async () => {
80+
expect(getPreferredLanagugesFor).toBeInstanceOf(Function)
81+
82+
result = await getPreferredLanagugesFor(alice)
83+
84+
expect(result).toEqual(['el']) // toMatchSnapshot()
85+
})
86+
})
87+
//

0 commit comments

Comments
 (0)