moved findAddressBook back to the class
MODIFY
package.json
package.json
],
"scripts": {
"build": "npm run clean && npm run build-version && npm run build-lib && npm run build-types && npm run build-webpack",
- "build-lib": "babel src -d lib --source-maps --extensions '.ts,.js'",
+ "build-lib": "babel src -d lib --source-maps --extensions \".ts,.js\"",
"build-types": "tsc --emitDeclarationOnly",
"build-version": "sh ./timestamp.sh > src/versionInfo.ts && eslint \"src/versionInfo.ts\" --fix",
"build-webpack": "webpack",
- "watch": "npm run build-version && babel src --out-dir lib --watch --source-maps --extensions '.ts,.js'",
+ "watch": "npm run build-version && babel src --out-dir lib --watch --source-maps --extensions \".ts,.js\"",
"clean": "rm -rf lib",
"coverage": "jest --coverage --collectCoverageFrom=src/**/*.[jt]s",
"lint": "eslint \"src/**/*.js\" \"src/**/*.ts\" \"test/**/*.ts\"",

MODIFY
src/widgets/peoplePicker.js
src/widgets/peoplePicker.js
@@ -28,6 +28,7 @@ export class PeoplePicker {
this.groupPickedCb = groupPickedCb
this.selectedgroup = this.options.selectedgroup // current selected group if any
this.onSelectGroup = this.onSelectGroup.bind(this)
+ this.findAddressBook = this.findAddressBook.bind(this)
}
render () {
@@ -50,7 +51,7 @@ export class PeoplePicker {
container.appendChild(selectedGroup)
container.appendChild(changeGroupButton)
} else {
- findAddressBook(this.typeIndex)
+ this.findAddressBook(this.typeIndex)
.then(({ book }) => {
const chooseExistingGroupButton = document.createElement('button')
chooseExistingGroupButton.textContent = escape(
@@ -105,6 +106,40 @@ export class PeoplePicker {
return this
}
+ findAddressBook (typeIndex) {
+ return new Promise((resolve, reject) => {
+ kb.fetcher.nowOrWhenFetched(typeIndex, (ok, err) => {
+ if (!ok) {
+ return reject(err)
+ }
+ const bookRegistration = kb.any(
+ null,
+ ns.solid('forClass'),
+ ns.vcard('AddressBook')
+ )
+ if (!bookRegistration) {
+ return reject(
+ new Error(
+ 'no address book registered in the solid type index ' + typeIndex
+ )
+ )
+ }
+ const book = kb.any(bookRegistration, ns.solid('instance'))
+ if (!book) {
+ return reject(new Error('incomplete address book registration'))
+ }
+ kb.fetcher
+ .load(book)
+ .then(function (_xhr) {
+ return resolve({ book })
+ })
+ .catch(function (err) {
+ return reject(new Error('Could not load address book ' + err))
+ })
+ })
+ })
+ }
+
onSelectGroup (group) {
this.selectedgroup = group
this.groupPickedCb(group)
@@ -440,41 +475,6 @@ function indexes(book) {
groupContainer: kb.sym(book.dir().uri + 'Group/')
}
}
-// Below are functions that are exported to make testing easier
-// @ignore exporting this only for the unit test
-export function findAddressBook(typeIndex) {
- return new Promise((resolve, reject) => {
- kb.fetcher.nowOrWhenFetched(typeIndex, (ok, err) => {
- if (!ok) {
- return reject(err)
- }
- const bookRegistration = kb.any(
- null,
- ns.solid('forClass'),
- ns.vcard('AddressBook')
- )
- if (!bookRegistration) {
- return reject(
- new Error(
- 'no address book registered in the solid type index ' + typeIndex
- )
- )
- }
- const book = kb.any(bookRegistration, ns.solid('instance'))
- if (!book) {
- return reject(new Error('incomplete address book registration'))
- }
- kb.fetcher
- .load(book)
- .then(function(_xhr) {
- return resolve({ book })
- })
- .catch(function(err) {
- return reject(new Error('Could not load address book ' + err))
- })
- })
- })
-}
export function createNewGroup (book, defaultNewGroupName) {
const { groupIndex, groupContainer } = indexes(book)

MODIFY
test/unit/widgets/peoplePicker.test.ts
test/unit/widgets/peoplePicker.test.ts
@@ -7,7 +7,6 @@ import {
Group,
GroupBuilder,
Person,
- findAddressBook,
createNewGroup
} from '../../../src/widgets/peoplePicker'
import ns from '../../../src/ns'
@@ -21,10 +20,10 @@ const element = dom.createElement('div')
describe('FindAddressBook', () => {
it('exists', () => {
- expect(findAddressBook).toBeInstanceOf(Function)
+ expect(new PeoplePicker().findAddressBook).toBeInstanceOf(Function)
})
it('runs', () => {
- expect(findAddressBook('typeIndex')).toMatchObject({})
+ expect(new PeoplePicker().findAddressBook('typeIndex')).toMatchObject({})
})
it('should call kb.any and spy load when callback is successful ', () => {
@@ -35,7 +34,7 @@ describe('FindAddressBook', () => {
.mockReturnValueOnce('book')
.mockReturnValueOnce('book')
const spyLoad = jest.spyOn(fetcher, 'load').mockResolvedValue('book')
- findAddressBook('typeIndex')
+ new PeoplePicker().findAddressBook('typeIndex')
const callback: any = spyOnNowOrWhenFetched.mock.calls[0][1]
callback(true, undefined)
@@ -51,7 +50,7 @@ describe('FindAddressBook', () => {
.mockReturnValue('book')
.mockReturnValueOnce('book')
const spyLoad = jest.spyOn(fetcher, 'load').mockResolvedValue('book')
- findAddressBook('typeIndex')
+ new PeoplePicker().findAddressBook('typeIndex')
const callback: any = spyOnNowOrWhenFetched.mock.calls[0][1]
callback(false, undefined)
// expect(spyOnNowOrWhenFetched).toThrowError()
@@ -63,7 +62,7 @@ describe('FindAddressBook', () => {
.mockReturnValueOnce(null)
.mockReturnValueOnce('book')
const spyLoad = jest.spyOn(fetcher, 'load').mockResolvedValue('book')
- findAddressBook('typeIndex')
+ new PeoplePicker().findAddressBook('typeIndex')
const callback: any = spyOnNowOrWhenFetched.mock.calls[0][1]
callback(true, undefined)
// expect(spyOnNowOrWhenFetched).toThrowError()
@@ -76,7 +75,7 @@ describe('FindAddressBook', () => {
.mockReturnValueOnce('bookregs')
.mockReturnValueOnce(null)
const spyLoad = jest.spyOn(fetcher, 'load').mockResolvedValue('book')
- findAddressBook('typeIndex')
+ new PeoplePicker().findAddressBook('typeIndex')
const callback: any = spyOnNowOrWhenFetched.mock.calls[0][1]
callback(true, undefined)
// expect(spyOnNowOrWhenFetched).toThrowError()
@@ -91,7 +90,7 @@ describe('FindAddressBook', () => {
.mockReturnValue('book')
.mockReturnValue('book')
const spyLoad = jest.spyOn(fetcher, 'load').mockRejectedValue(new Error())
- findAddressBook('typeIndex')
+ new PeoplePicker().findAddressBook('typeIndex')
const callback: any = spyOnNowOrWhenFetched.mock.calls[0][1]
callback(true, undefined)
// expect(spyOnNowOrWhenFetched).toThrowError()

moved findAddressBook back to the class