Skip to content

Commit

Permalink
refactor: Use the setup() pattern for tests to remove boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
ptbrowne committed May 9, 2019
1 parent 3e92266 commit 9ceb2cb
Showing 1 changed file with 20 additions and 32 deletions.
52 changes: 20 additions & 32 deletions test/lib/stack-client/stack-client.init.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,24 @@ const { init } = stack

describe('stack client', () => {
describe('init', () => {
let cozyClient = new CozyClient({
stackClient: {
let cozyClient, params

const setup = async({ isLogged, isPublic }) => {
if (isLogged === undefined) {
throw new Error("Please define explicity isLogged in your tests.")
}
cozyClient = new CozyClient({
token: { token: 'mytoken' },
uri: 'https://test.mycozy.cloud'
})
cozyClient.isLogged = isLogged
params = {
cozyClient,
isPublic,
onCreate: function() {},
onDelete: function() {}
}
})
let params = {
cozyClient,
onCreate: function() {},
onDelete: function() {}
await init(params)
}

beforeAll(async () => {
Expand All @@ -31,44 +39,24 @@ describe('stack client', () => {
})

it('should not called internal client', async () => {
await init(params)
await setup({ isLogged: false })
expect(internal.init).not.toHaveBeenCalled()
})

it('should not have initialized the realtime if the user is not logged', async () => {
await init({
...params,
cozyClient: {
...cozyClient,
isLogged: false
}
})
await setup({ isLogged: false })
expect(initializeRealtime).toHaveBeenCalledTimes(0)
})

it('should not have initialized the realtime if the user is not logged even if isPublic is set to false', async () => {
await init({
...params,
isPublic: false,
cozyClient: {
...cozyClient,
isLogged: false
}
})
await setup({ isLogged: false, isPublic: false})
expect(initializeRealtime).toHaveBeenCalledTimes(0)
})

it('should have initialized the realtime is the user is logged', async () => {
const client = {
...cozyClient,
isLogged: true
}
await init({
...params,
cozyClient: client
})
await setup({ isLogged: true })
expect(initializeRealtime).toHaveBeenCalled()
expect(initializeRealtime.mock.calls[0][0].cozyClient).toBe(client)
expect(initializeRealtime.mock.calls[0][0].cozyClient).toBe(cozyClient)
})
})
})

0 comments on commit 9ceb2cb

Please sign in to comment.