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 27, 2019
1 parent e61a99f commit e940330
Showing 1 changed file with 19 additions and 31 deletions.
50 changes: 19 additions & 31 deletions test/lib/stack-client/stack-client.init.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,56 +10,44 @@ 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)
}

afterAll(() => {
jest.restoreAllMocks()
})

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 e940330

Please sign in to comment.