Skip to content

Commit

Permalink
feat: Inject cozy-client - stage 12 - new client is independant
Browse files Browse the repository at this point in the history
* Finish the `init` function so it directly initializes the realtime
* Do not initialize or reference the old internal client anymore
  • Loading branch information
edas committed Feb 16, 2019
1 parent d5ca50d commit f6b6586
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
25 changes: 14 additions & 11 deletions src/lib/stack-client.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* global __TARGET__ */
/* eslint-env browser */

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

import {
ForbiddenException,
Expand Down Expand Up @@ -333,20 +333,23 @@ const cozyFetchJSON = function(cozy, method, path, body) {
* @param {Function} arg.onDeleteApp
* @returns {Promise}
*/
const init = function(options) {
cozyClient = options.cozyClient
const legacyOptions = {
...options,
cozyURL: getCozyURLOrigin(),
token: getStackClient().token.token
}
return internal.init(legacyOptions)
const init = function({
cozyClient: client,
onCreateApp,
onDeleteApp
}) {
cozyClient = client
return initializeRealtime({
getApp,
onCreateApp,
onDeleteApp,
token: getStackClient().token.token,
url: getCozyURLOrigin(),
})
}

export default {
...internal,
get: {
...internal.get,
app: getApp,
apps: getApps,
context: withCache(getContext, {}),
Expand Down
24 changes: 10 additions & 14 deletions test/lib/stack-client/stack-client.init.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import stack from 'lib/stack-client'

import internal from 'lib/stack-internal'
import initializeRealtime from 'lib/realtime'

jest.mock('lib/realtime');
initializeRealtime.mockResolvedValue(Promise.resolve())

let internalInit

Expand Down Expand Up @@ -35,23 +39,15 @@ describe("stack client", () => {
internal.init = internalInit
})

it("should called internal client", () => {
expect( internal.init ).toHaveBeenCalled()
})

it("should have set the cozy-client token", () => {
expect( internal.init.mock.calls[0][0].token ).toBe("mytoken")
it("should not called internal client", () => {
expect( internal.init ).not.toHaveBeenCalled()
})

it("should have set the cozy-client uri", () => {
expect( internal.init.mock.calls[0][0].cozyURL ).toBe("https://test.mycozy.cloud")
it("should have initialized the realtime", () => {
expect( initializeRealtime ).toHaveBeenCalled()
expect( initializeRealtime.mock.calls[0][0].token ).toBe("mytoken")
expect( initializeRealtime.mock.calls[0][0].url ).toBe("https://test.mycozy.cloud")
})

it("should pass onCreateApp and onDeleteApp functions", () => {
expect( internal.init.mock.calls[0][0].onDeleteApp ).toBeInstanceOf(Function)
expect( internal.init.mock.calls[0][0].onCreateApp ).toBeInstanceOf(Function)
})

})

})

0 comments on commit f6b6586

Please sign in to comment.