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 8, 2019
1 parent 87080f5 commit 3a0d235
Showing 1 changed file with 30 additions and 32 deletions.
62 changes: 30 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,18 +10,31 @@ const { init } = stack

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

const setup = async() => {
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)
}

beforeEach(() => {
isLogged = undefined
isPublic = undefined
})

beforeAll(async () => {
jest.spyOn(internal, 'init').mockResolvedValue(undefined)
})
Expand All @@ -31,44 +44,29 @@ describe('stack client', () => {
})

it('should not called internal client', async () => {
await init(params)
isLogged = false
await setup()
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
}
})
isLogged = false
await setup()
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
}
})
isLogged = false
isPublic = false
await setup()
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
})
isLogged = true
await setup()
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 3a0d235

Please sign in to comment.