Skip to content

Commit 660ab45

Browse files
tests for fieldStore and fieldLabel
1 parent 0dd52cc commit 660ab45

2 files changed

Lines changed: 77 additions & 16 deletions

File tree

src/widgets/forms/basic.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@ import { mostSpecificClassURI } from './fieldFunction'
99
import { fieldParams } from './fieldParams'
1010

1111
/**
12+
* Create an anchor element with a label as the anchor text.
13+
*
14+
* @param dom The DOM
15+
* @param property href for the anchor element
16+
* @param fieldInQuestion field to produce a label for
17+
*
1218
* @ignore exporting this only for unit tests
1319
*/
14-
export function fieldLabel (dom: HTMLDocument, property: NamedNode, form: Node): HTMLElement | Text {
15-
let lab = store.any(form, ns.ui('label'))
20+
export function fieldLabel (dom: HTMLDocument, property: NamedNode | undefined, fieldInQuestion: Node): HTMLElement | Text {
21+
let lab = store.any(fieldInQuestion, ns.ui('label'))
1622
if (!lab) lab = label(property, true) // Init capital
1723
if (property === undefined) {
1824
return dom.createTextNode('@@Internal error: undefined property')

test/unit/widgets/forms/basic.test.ts

Lines changed: 69 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,57 @@ import {
44
fieldStore,
55
basicField
66
} from '../../../../src/widgets/forms/basic'
7+
import { store, ns } from '../../../../src/'
8+
import { clearStore } from '../../helpers/clearStore'
9+
10+
afterEach(clearStore)
711

812
describe('fieldLabel', () => {
913
it('exists', () => {
10-
expect(fieldLabel).toBeInstanceOf(Object)
14+
expect(fieldLabel).toBeInstanceOf(Function)
1115
})
12-
it('runs', () => {
13-
// expect(fieldLabel(document, namedNode('http://example.com/#this'), null)).toBeInstanceOf(
14-
// HTMLAnchorElement
15-
// )
16+
const predicates = [
17+
ns.ui('label'),
18+
ns.link('message'),
19+
ns.vcard('fn'),
20+
ns.foaf('name'),
21+
ns.dct('title'),
22+
ns.dc('title'),
23+
ns.rss('title'),
24+
ns.contact('fullName'),
25+
namedNode('http://www.w3.org/2001/04/roadmap/org#name'),
26+
ns.cal('summary'),
27+
ns.foaf('nick'),
28+
ns.rdfs('label')
29+
]
30+
for (let i = 0; i < predicates.length; i++) {
31+
it(`checks for ${predicates[i]}`, () => {
32+
for (let j = i; j < predicates.length; j++) {
33+
store.add(namedNode('http://example.com/#this'), predicates[j], `from ${predicates[j]}`, namedNode('http://example.com/'))
34+
}
35+
const anchorElement = fieldLabel(document, namedNode('http://example.com/#this'), namedNode('http://example.com/#this'))
36+
const firstChar = (i === 0 ? 'f' : 'F')
37+
const expectedLabel = `${firstChar}rom &lt;${predicates[i].uri}&gt;`
38+
expect((anchorElement as HTMLElement).innerHTML).toEqual(expectedLabel)
39+
})
40+
}
41+
it('sets the property as the href', () => {
42+
store.add(namedNode('http://example.com/#this'), predicates[0], `from ${predicates[0]}`, namedNode('http://example.com/'))
43+
const anchorElement = fieldLabel(document, namedNode('http://example.com/#the-property'), namedNode('http://example.com/#this'))
44+
const expectedHref = 'http://example.com/#the-property'
45+
expect((anchorElement as HTMLAnchorElement).href).toEqual(expectedHref)
46+
})
47+
it('sets the style', () => {
48+
store.add(namedNode('http://example.com/#this'), predicates[0], `from ${predicates[0]}`, namedNode('http://example.com/'))
49+
const anchorElement = fieldLabel(document, namedNode('http://example.com/#the-property'), namedNode('http://example.com/#this'))
50+
const expectedStyle = 'color: #3B5998; text-decoration: none;'
51+
expect((anchorElement as HTMLAnchorElement).getAttribute('style')).toEqual(expectedStyle)
52+
})
53+
it('shows an error if property is undefined', () => {
54+
store.add(namedNode('http://example.com/#this'), predicates[0], `from ${predicates[0]}`, namedNode('http://example.com/'))
55+
const textNode = fieldLabel(document, undefined, namedNode('http://example.com/#this'))
56+
const expectedText = '@@Internal error: undefined property'
57+
expect((textNode as Text).wholeText).toEqual(expectedText)
1658
})
1759
it.skip(' ...', () => {
1860
// expect(fieldLabel('document', undefined, 'form').toBe())
@@ -23,16 +65,29 @@ describe('fieldStore', () => {
2365
it('exists', () => {
2466
expect(fieldStore).toBeInstanceOf(Object)
2567
})
26-
it.skip('runs', () => {
27-
// expect(fieldStore(null, null, null)).toEqual({
28-
// termType: 'NamedNode',
29-
// value: 'http://example.com/'
30-
// })
68+
it('returns the first matching why, if editable', () => {
69+
store.add(namedNode('http://example.com/#s'), namedNode('http://example.com/#p'), namedNode('http://example.com/#o1'), namedNode('http://why1.com/'))
70+
store.add(namedNode('http://example.com/#s'), namedNode('http://example.com/#p'), namedNode('http://example.com/#o2'), namedNode('http://why2.com/'))
71+
72+
expect(fieldStore(namedNode('http://example.com/#s'), namedNode('http://example.com/#p'), namedNode('http://example.com/#def'))).toEqual({
73+
termType: 'NamedNode',
74+
value: 'http://why1.com/'
75+
})
76+
})
77+
it('returns def if the first matching why is not editable', () => {
78+
store.add(namedNode('http://example.com/#s'), namedNode('http://example.com/#p'), namedNode('http://example.com/#o1'), 'some literal 1')
79+
store.add(namedNode('http://example.com/#s'), namedNode('http://example.com/#p'), namedNode('http://example.com/#o2'), 'some literal 2')
80+
81+
expect(fieldStore(namedNode('http://example.com/#s'), namedNode('http://example.com/#p'), namedNode('http://def.com/'))).toEqual({
82+
termType: 'NamedNode',
83+
value: 'http://def.com/'
84+
})
3185
})
32-
it.skip('returns def when there is no matching statement', () => {
33-
// const statementMatching = jest.fn()
34-
// statementMatching.mockReturnValueOnce(null)
35-
// expect(fieldStore('subject', 'predicate', 'def')).toBe('def')
86+
it('returns def when there is no matching statement', () => {
87+
expect(fieldStore(namedNode('http://example.com/#s'), namedNode('http://example.com/#p'), namedNode('http://def.com/'))).toEqual({
88+
termType: 'NamedNode',
89+
value: 'http://def.com/'
90+
})
3691
})
3792
})
3893

0 commit comments

Comments
 (0)