Skip to content

Commit f49ff89

Browse files
committed
Exported Person began tests refined findAddressBook Tests
1 parent 29675b0 commit f49ff89

2 files changed

Lines changed: 76 additions & 37 deletions

File tree

src/widgets/peoplePicker.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,10 @@ export class GroupBuilder {
401401
return Promise.all(updatePromises)
402402
}
403403
}
404-
405-
class Person {
404+
// @ignore exporting this only for the unit test
405+
// @@ TODO maybe I should move this down at end, but for
406+
// now I will leave it where it was
407+
export class Person {
406408
constructor(element, webIdNode, handleRemove) {
407409
this.webIdNode = webIdNode
408410
this.element = element

test/unit/widgets/peoplePicker.test.ts

Lines changed: 72 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ import {
66
GroupPicker,
77
Group,
88
GroupBuilder,
9+
Person,
910
findAddressBook
1011
} from '../../../src/widgets/peoplePicker'
11-
import { resolve } from 'dns'
12-
import { SolidAuth } from 'solid-auth-client'
13-
import { callbackify } from 'util'
12+
1413
jest.mock('rdflib')
1514
jest.mock('solid-auth-client')
1615
const kb = require('../../../src/store')
@@ -57,38 +56,54 @@ describe('FindAddressBook', () => {
5756
const spyOnNowOrWhenFetched = jest.spyOn(fetcher, 'nowOrWhenFetched')
5857
const spyAny = jest
5958
.spyOn(kb, 'any')
60-
.mockReturnValue(null)
59+
.mockReturnValue('book')
6160
.mockReturnValueOnce('book')
6261
const spyLoad = jest.spyOn(fetcher, 'load').mockResolvedValue('book')
6362
findAddressBook('typeIndex')
6463
const callback: any = spyOnNowOrWhenFetched.mock.calls[0][1]
6564
callback(false, undefined)
6665
// expect(spyOnNowOrWhenFetched).toThrowError()
6766
})
68-
it('should return an error if it does not return a book', () => {
67+
it('should return an error if it does not return a book registration', () => {
6968
const spyOnNowOrWhenFetched = jest.spyOn(fetcher, 'nowOrWhenFetched')
7069
const spyAny = jest
7170
.spyOn(kb, 'any')
72-
.mockReturnValue('book')
7371
.mockReturnValueOnce(null)
72+
.mockReturnValueOnce('book')
7473
const spyLoad = jest.spyOn(fetcher, 'load').mockResolvedValue('book')
7574
findAddressBook('typeIndex')
7675
const callback: any = spyOnNowOrWhenFetched.mock.calls[0][1]
7776
callback(true, undefined)
7877
// expect(spyOnNowOrWhenFetched).toThrowError()
7978
})
80-
it('should ....', () => {
79+
80+
it('should throw an error when there is no book', () => {
8181
const spyOnNowOrWhenFetched = jest.spyOn(fetcher, 'nowOrWhenFetched')
8282
const spyAny = jest
8383
.spyOn(kb, 'any')
84-
.mockReturnValue(null)
85-
.mockReturnValueOnce('book')
84+
.mockReturnValueOnce('bookregs')
85+
.mockReturnValueOnce(null)
8686
const spyLoad = jest.spyOn(fetcher, 'load').mockResolvedValue('book')
8787
findAddressBook('typeIndex')
8888
const callback: any = spyOnNowOrWhenFetched.mock.calls[0][1]
8989
callback(true, undefined)
9090
// expect(spyOnNowOrWhenFetched).toThrowError()
9191
})
92+
it('should throw an error when there an error with the load', () => {
93+
// this was working before I changed the above to mockReturnValueOnce
94+
// which I needed to do in order to get that test to take affect...
95+
// need to research
96+
const spyOnNowOrWhenFetched = jest.spyOn(fetcher, 'nowOrWhenFetched')
97+
const spyAny = jest
98+
.spyOn(kb, 'any')
99+
.mockReturnValue('book')
100+
.mockReturnValue('book')
101+
const spyLoad = jest.spyOn(fetcher, 'load').mockRejectedValue(new Error())
102+
findAddressBook('typeIndex')
103+
const callback: any = spyOnNowOrWhenFetched.mock.calls[0][1]
104+
callback(true, undefined)
105+
// expect(spyOnNowOrWhenFetched).toThrowError()
106+
})
92107
})
93108
describe('PeoplePicker', () => {
94109
it('exists', () => {
@@ -127,20 +142,7 @@ describe('PeoplePicker.render', () => {
127142
}
128143
`)
129144
})
130-
// THIS IS THE ONE I AM WORKING ON.
131-
// IF YOU CHECK THE CODE IN PEOPLEPICKER.JS YOU WILL SEE THAT IT CALLS
132-
// FINDADDRESSBOOK .. HOW CAN I REACH THE CODE IN THE .THEN
133-
// SO I WAS THINKING BY RESOLVING KB.FETCHER.NOWORWHENFETCHED.. THIS
134-
// SHOUDLD RETURN A BOOK SO THAT THE CODE IN THIS SECTION CAN BE
135-
// EXECUTED AND TESTED. I CAN'T SEEM TO MOCK NOWORWHENFETCHED. I'VE GOTTEN CLOSER
136-
// I THINK BY ADDING THIS CODE ABOVE const fetcher = kb.fetcher. THIS AT
137-
// LEAST LET ME NOW WRITE THE MOCK WITHOUT ERROR, BUT IT STILL ISN'T USING IT
138-
// @@ TODO can't seem to get the findAddressBook to return anything even
139-
// when giving a typeIndex. I am certain the typeIndex would be incorrect however
140-
// I'm not sure of the correct one
141-
/* ways I have tried const spyOnNowOrWhenFetched = jest
142-
.spyOn(fetcher, 'nowOrWhenFetched')
143-
.mockReturnValueOnce(Promise.resolve('book')) */
145+
144146
it('.. type index ...', () => {
145147
const typeIndex = 'publicTypeIndex'
146148
const groupPickedCb = () => {}
@@ -153,13 +155,6 @@ describe('PeoplePicker.render', () => {
153155
options
154156
)
155157

156-
/* const mockFindAddressBook: jest.Mock = require.requireMock(
157-
'../../../src/widgets/peoplePicker'
158-
).findAddressBook
159-
mockFindAddressBook.mockResolvedValueOnce('book') */
160-
/* jest.mock('rdflib', () => {
161-
nowOrWhenFetched: jest.fn(() => Promise.resolve('book'))
162-
}) */
163158
const spyAny = jest
164159
.spyOn(kb, 'any')
165160
.mockReturnValueOnce('book')
@@ -434,6 +429,7 @@ describe('GroupBuilder.render', () => {
434429
expect(new GroupBuilder().render).toBeInstanceOf(Function)
435430
})
436431
it('runs', () => {
432+
jest.clearAllMocks()
437433
const groupArg = RdfLib.sym('')
438434
const book = RdfLib.sym('')
439435
const handler = () => {}
@@ -469,11 +465,6 @@ GroupBuilder {
469465
type="text"
470466
/>
471467
</label>
472-
<p>
473-
474-
To add someone to this group, drag and drop their WebID URL onto the box.
475-
476-
</p>
477468
<button>
478469
Done
479470
</button>
@@ -574,3 +565,49 @@ describe('GroupBuilder.setGroupName', () => {
574565
expect(groupBuilder.setGroupName()).toMatchInlineSnapshot('Promise {}')
575566
})
576567
})
568+
569+
describe('Person', () => {
570+
it('exists', () => {
571+
expect(new Person()).toBeInstanceOf(Person)
572+
})
573+
})
574+
describe('Person.render', () => {
575+
it('exists', () => {
576+
expect(new Person().render).toBeInstanceOf(Function)
577+
})
578+
it('runs', () => {
579+
// @@ TODO Ask Michiel or Vince about what a proper WebIdNode should be
580+
const webIdNode = document.createElement('div')
581+
const element = document.createElement('div')
582+
const handleRemove = true
583+
const person = new Person(webIdNode, element, handleRemove)
584+
expect(person.render()).toMatchInlineSnapshot(`
585+
Person {
586+
"element": <div>
587+
<div
588+
style="display: flex;"
589+
>
590+
<img
591+
height="50"
592+
src="undefined"
593+
style="margin: 5px;"
594+
width="50"
595+
/>
596+
<span
597+
style="flex-grow: 1; margin: auto 0px;"
598+
>
599+
undefined
600+
</span>
601+
<button
602+
style="margin: 5px;"
603+
>
604+
Remove
605+
</button>
606+
</div>
607+
</div>,
608+
"handleRemove": true,
609+
"webIdNode": <div />,
610+
}
611+
`)
612+
})
613+
})

0 commit comments

Comments
 (0)