joeferraro / react-native-cookies

Cookie manager for React Native
MIT License
788 stars 315 forks source link

"Invariant Violation" when running Tests #88

Open simonarcher opened 6 years ago

simonarcher commented 6 years ago

So I believe this has been already brought up in the another issue ( https://github.com/joeferraro/react-native-cookies/issues/16 ), but none of the solutions seems to have resolved the issue for me and looking for additional support if possible.

If I run Jest tests as is, i get the following error

Invariant Violation: react-native-cookies: Add RNCookieMangerIOS.h and RNCookieManagerIOS.m to your Xcode project

  at invariant (node_modules/invariant/invariant.js:42:7)
  at Object.<anonymous> (node_modules/react-native-cookies/index.js:10:1)`

If I modify my tests 'Setup file' with the following

  jest.mock('react-native-cookies', () => {
    return {
      addEventListener: jest.fn(),
      removeEventListener: jest.fn(),
      openURL: jest.fn(),
      canOpenURL: jest.fn(),
     getInitialURL: jest.fn(),
    }
  })

I get the following error

TypeError: CookieManager.get is not a function

I presume this is because 'get' isn't mocked in the file? (This is where things become vague for me)

If I add the 'get' function to the mock, like this

jest.mock('react-native-cookies', () => {
    return {
      addEventListener: jest.fn(),
      removeEventListener: jest.fn(),
      openURL: jest.fn(),
      canOpenURL: jest.fn(),
      getInitialURL: jest.fn(),
      get: jest.fn(),
      }
  })

I get the error

TypeError: Cannot read property 'then' of undefined

Anybody able to help identify where Im going wrong here? Not sure how to progress with my tests because of this.

(If this issue is an unecessary duplication, please feel free to close and we can continue the discussion in the other one :) )

Tang-jianchao commented 6 years ago

same as you,

fvoordeckers commented 6 years ago

This is working for me:

jest.mock('react-native-cookies', () => ({
    addEventListener: jest.fn(),
    removeEventListener: jest.fn(),
    openURL: jest.fn(),
    canOpenURL: jest.fn(),
    getInitialURL: jest.fn(),
    get: () => Promise.resolve(null),
}));

You can't return a normal function since get returns a promise, and therefore is thenable.

safaiyeh commented 4 years ago

I have forked the repo here: https://github.com/safaiyeh/react-native-cookie-store We can create issues and PRs there to continue the development of the project.