osamaqarem/react-native-image-colors

Not able to test await ImageColors.getColors using jest

Closed this issue · 1 comments

I'm not able to test await ImageColors.getColors method. I tried following approaches:

Code :
const fileContent = await RNFS.readFile(${imageSrc}, 'base64');
console.log('file',fileContent); // it prints in all the cases whether it's test case or app debugging.
const imageColor = await ImageColors.getColors(data:image/jpeg;base64,${fileContent}, {
fallback: DEFAULT_LAYER_COLOR,
});
console.log('imageColor',imageColor); // when running the test case it never prints, test fails with a timeout error.

Mocking the method :

jest.mock('react-native-image-colors', () => ({
getColors: jest.fn().mockReturnValue(Promise.resolve({
background: '#000000',
primary: '#d4d4d4',
secondary: '#e4e4e4',
detail: '#b4b4b4',
quality: 'high',
platform: 'ios',
})),
}));

OR

jest.mock('react-native-image-colors', () => ({
getColors: jest.fn(),
}));

Asserting the method call in different ways:

  1. expect(ImageColors.getColors as jest.Mock).toHaveBeenCalled();
  2. const getColorsSpy = jest.spyOn(ImageColors, 'getColors');
    getColorsSpy.mockReturnValue(Promise.resolve(colors)); // colors = constant
  3. (ImageColors.getColors as jest.Mock).mockReturnValue(Promise.resolve(color));
  4. await (ImageColors.getColors as jest.Mock).mockImplementation(() => Promise.resolve('ss'));

But none of them works. All of them results in a timeout failure.

jetaix commented

mock the getColors function works on my side.

jest.mock('react-native-image-colors', () => ({
  getColors: jest.fn().mockResolvedValue(''),
}));