Skip to content

Commit

Permalink
feat: Inject cozy-client - stage 10 - get.iconProps
Browse files Browse the repository at this point in the history
  • Loading branch information
edas committed Feb 20, 2019
1 parent 7998fe7 commit 4ebc20a
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/lib/stack-client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
/* global __TARGET__ */
/* eslint-env browser */

import internal from 'lib/stack-internal.js'
import getIcon from 'lib/icon'

import {
ForbiddenException,
Expand Down Expand Up @@ -248,6 +252,45 @@ const getStorageData = function() {
)
}

/**
* Fetch an icon data from its path
*
* The purpose of this function is to be sent
* to AppIcon components for mobile devices.
*
* @private
* @function
* @param {string} iconPath - path of the icon in the stack
* @returns {Blob}
*/
const iconFetcher = function(iconPath) {
return getStackClient().fetch('GET', iconPath)
}

/**
* Get a props object that can be sent to an AppIcon component
*
* Mobile devices and web browsers need different props
*
* @function
* @returns {Object}
*/
const getAppIconProps = function() {
const isMobile = (__TARGET__ === 'mobile')

const mobileAppIconProps = {
fetchIcon: app => getIcon(iconFetcher, app, true)
}

const browserAppIconProps = {
// we mustn't give the protocol here
domain: getCozyURL().host,
secure: (getCozyURL().protocol === 'https:')
}

return isMobile ? mobileAppIconProps : browserAppIconProps
}

/**
* Get settings context
*
Expand Down Expand Up @@ -287,6 +330,7 @@ export default {
apps: getApps,
context: withCache(getContext, {}),
storageData: getStorageData,
iconProps: getAppIconProps,
cozyURL: getCozyURLOrigin
},
updateAccessToken,
Expand Down
84 changes: 84 additions & 0 deletions test/lib/stack-client/stack-client.appiconprops.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/* global __TARGET__ */

import stack from 'lib/stack-client'

import internal from 'lib/stack-internal'

let oldTarget

describe("stack client", () => {

describe("getAppIconProps", () => {

const stackClient = {
token: { token: "mytoken"},
uri: "https://test.mycozy.cloud",
}

const cozyClient = {
getStackClient: () => stackClient
}

const params = {
cozyClient,
onCreateApp: function() {},
onDeleteApp: function() {},
}

beforeAll(async () => {
oldTarget = global.__TARGET__
jest.spyOn(internal.get, 'iconProps').mockResolvedValue(undefined)
await stack.init(params)
})

afterAll(() => {
jest.restoreAllMocks()
global.__TARGET__ = oldTarget
})

it("should not forward to the old internal client", () => {
stack.get.iconProps()
expect( internal.get.iconProps ).not.toHaveBeenCalled()
})

describe("for target=browser", () => {

beforeAll(() => {
global.__TARGET__ = "browser"
})

it("should have `domain` and `secure` set", () => {
const data = stack.get.iconProps()
expect( data.domain ).toBe("test.mycozy.cloud")
expect( data.secure ).toBe(true)
})

it("should not have a `fetchIcon` function", () => {
const data = stack.get.iconProps()
expect( data.fetchIcon ).toBe(undefined)
})

})

describe("for target=mobile", () => {

beforeAll(() => {
global.__TARGET__ = "mobile"
})

it("should note have `domain` and `secure` set", () => {
const data = stack.get.iconProps()
expect( data.domain ).toBeUndefined()
expect( data.secure ).toBeUndefined()
})

it("should have a `fetchIcon` function set", () => {
const data = stack.get.iconProps()
expect( data.fetchIcon ).toBeInstanceOf(Function)
})

})

})

})

0 comments on commit 4ebc20a

Please sign in to comment.