keajs / kea

Batteries Included State Management for React
https://keajs.org/
MIT License
1.94k stars 51 forks source link

Unit Test in 2.4 #131

Closed danielmariz closed 3 years ago

danielmariz commented 3 years ago

I'm writing simple tests based on the examples from the documentation, I thought I was missing something from my logic import so I copied the example from the docs page which also does not pass. The actions calls just don't change any value of the reducer. Is it something on the latest version? Code example:

const authLogic = kea<authLogicType<CredentialsType, Auth0Error>>({
    path: ['containers', 'user', 'auth'],

    actions: {
        requestAuthentication: (credentials: CredentialsType) => ({ credentials }),
        authenticationFailed: (response:Auth0Error) => ({response}),
    },
    reducers: {
        isAuthenticating: [
            false as boolean,
            {
                requestAuthentication: () => true,
                authenticationFailed: () => false,
            }
        ],
    }
})

export default authLogic

Test example:

import authLogic from '../authLogic';

beforeEach(() => {
    resetContext({ createStore: true });
});

test('isAuthenticating value should change based on actions calls', async () => {
    authLogic.mount()
    const {isAuthenticating} = authLogic.values
    const {requestAuthentication} = authLogic.actions
    expect(isAuthenticating).toBe(false)
    requestAuthentication({email: 'test@test.org', password: '123456'})
    expect(isAuthenticating).toBe(true)
})

and the error printed:

 ● isAuthenticating value should change based on actions calls

    expect(received).toBe(expected) // Object.is equality

    Expected: true
    Received: false

      14 |     console.log(authLogic);
      15 |     
    > 16 |     expect(isAuthenticating).toBe(true)
danielmariz commented 3 years ago

Oh I got it, we can't destructure the values the way I did.

I had to do this

test('isAuthenticating value should change based on actions calls', async () => {
        authLogic.mount()
        const {requestAuthentication} = authLogic.actions
        expect(authLogic.values.isAuthenticating).toBe(false)
        requestAuthentication({email: 'test@test.org', password: '123456'})
        expect(authLogic.values.isAuthenticating).toBe(true)
    })