Skip to content

Conversation

@andrewda
Copy link
Member

Question Response
Version? all
Devices tested? n/a
Bug fix? no
New feature? no
Includes tests? yes
All Tests pass? yes*
Related ticket? #518

Description

Adds a test for the text-normalize file. Currently WIP.

@andrewda
Copy link
Member Author

Need to figure out how to reset the mock each time. The first run mockRequiredClasses values are carried on to all the remaining tests. Not sure how to fix this.

@coveralls
Copy link

coveralls commented Oct 28, 2017

Coverage Status

Coverage increased (+0.8%) to 47.811% when pulling 3d027d8 on test-normalize into dd2a50c on master.

@alejandronanez
Copy link
Member

@andrewda have you tried with beforeEach / afterEach?

@andrewda
Copy link
Member Author

@alejandronanez What should I use them for?

@alejandronanez
Copy link
Member

@andrewda I think you can reset the mock for every test that is run. (or I am just misunderstanding everything 😞 )

@andrewda
Copy link
Member Author

Yea, but I'm not sure how to reset just the Dimension and PixelRatio mocks, instead of everything (like the ones defined in testenv.js).

@machour
Copy link
Member

machour commented Mar 23, 2018

@andrewda I had a look at this PR and after a lot of trial and error, the only working thing I can come up with is chaining .mockReturnValueOnce() calls:

jest.mock('Dimensions', () => ({
  get: jest
    .fn()
    .mockReturnValueOnce(mockScreens.iPhone5)
    .mockReturnValueOnce(mockScreens.iPhone6),
}));
jest.mock('PixelRatio', () => ({
  get: jest
    .fn()
    .mockReturnValueOnce(2)
    .mockReturnValueOnce(2),
}));

describe('Normalize Text', () => {
  // iOS Devices

  it('should normalize correctly on iPhone 5', () => {
    const { normalize } = require('config');
    expect(normalize(1)).toEqual(0.95);
  });

  it('should normalize correctly on iPhone 6', () => {
    const { normalize } = require('config');
    expect(normalize(1)).toEqual(0.95);
  });

This require a change in the src/config/normalize-text.js file where Dimensions.get('window') is currently called twice (and as we mock once, a single test ends up consuming two mocks).

@chinesedfan
Copy link
Member

How about jest.unmock(moduleName)?

@machour
Copy link
Member

machour commented Mar 24, 2018

Didn't work for me. If you console.log() in mockClassWithGetter , you'll see the log only once

@chinesedfan
Copy link
Member

Jest@24 provides jest.isolateModules(fn). I think it is what we want.

chinesedfan added a commit to chinesedfan/git-point that referenced this pull request Oct 26, 2019
@andrewda
Copy link
Member Author

Closing in favor of #899 (thanks @chinesedfan!)

@andrewda andrewda closed this Oct 26, 2019
chinesedfan added a commit that referenced this pull request Oct 27, 2019
* test: cherry-pick files from #598

* test: jest.isolateModules works

* fix: device info

* fix: pass tests

* fix: consider as intervals
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants