From 4ece01b4cd3b9cd57229f0a001e899bb7f2f538b Mon Sep 17 00:00:00 2001 From: Marcin Kornek <7931279+marcinkornek@users.noreply.github.com> Date: Wed, 6 Sep 2023 08:52:44 +0200 Subject: [PATCH 1/2] feat: implement toHaveStyle with tests --- .../__snapshots__/to-have-style.test.tsx.snap | 31 +++++ src/matchers/__tests__/to-have-style.test.tsx | 111 ++++++++++++++++++ src/matchers/extend-expect.d.ts | 1 + src/matchers/extend-expect.ts | 2 + src/matchers/index.tsx | 1 + src/matchers/to-have-style.tsx | 79 +++++++++++++ 6 files changed, 225 insertions(+) create mode 100644 src/matchers/__tests__/__snapshots__/to-have-style.test.tsx.snap create mode 100644 src/matchers/__tests__/to-have-style.test.tsx create mode 100644 src/matchers/to-have-style.tsx diff --git a/src/matchers/__tests__/__snapshots__/to-have-style.test.tsx.snap b/src/matchers/__tests__/__snapshots__/to-have-style.test.tsx.snap new file mode 100644 index 000000000..0a89f7706 --- /dev/null +++ b/src/matchers/__tests__/__snapshots__/to-have-style.test.tsx.snap @@ -0,0 +1,31 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`handles negative test cases 1`] = ` +"expect(element).toHaveStyle() + +- Expected + + backgroundColor: blue; + transform: [ + { +- "scale": 1 ++ "scale": 2 ++ }, ++ { ++ "rotate": "45deg" + } + ];" +`; + +exports[`handles transform when transform undefined 1`] = ` +"expect(element).toHaveStyle() + +- Expected + +- transform: [ +- { +- "scale": 1 +- } +- ]; ++ transform: undefined;" +`; diff --git a/src/matchers/__tests__/to-have-style.test.tsx b/src/matchers/__tests__/to-have-style.test.tsx new file mode 100644 index 000000000..ee5cd58dc --- /dev/null +++ b/src/matchers/__tests__/to-have-style.test.tsx @@ -0,0 +1,111 @@ +import React from 'react'; +import { View, Text, StyleSheet, Pressable } from 'react-native'; +import { render } from '../..'; +import '../extend-expect'; + +test('handles positive test cases', () => { + const styles = StyleSheet.create({ + container: { borderBottomColor: 'white' }, + }); + const { getByTestId } = render( + + Hello World + + ); + + const container = getByTestId('container'); + + expect(container).toHaveStyle({ backgroundColor: 'blue', height: '100%' }); + expect(container).toHaveStyle([ + { backgroundColor: 'blue' }, + { height: '100%' }, + ]); + expect(container).toHaveStyle({ backgroundColor: 'blue' }); + expect(container).toHaveStyle({ height: '100%' }); + expect(container).toHaveStyle({ borderBottomColor: 'white' }); + expect(container).toHaveStyle({ width: '50%' }); + expect(container).toHaveStyle([[{ width: '50%' }]]); + expect(container).toHaveStyle({ + transform: [{ scale: 2 }, { rotate: '45deg' }], + }); +}); + +test('handles negative test cases', () => { + const { getByTestId } = render( + + Hello World + + ); + + const container = getByTestId('container'); + expect(() => + expect(container).toHaveStyle({ + backgroundColor: 'blue', + transform: [{ scale: 1 }], + }) + ).toThrowErrorMatchingSnapshot(); + expect(container).not.toHaveStyle({ fontWeight: 'bold' }); + expect(container).not.toHaveStyle({ color: 'black' }); + expect(container).not.toHaveStyle({ + transform: [{ rotate: '45deg' }, { scale: 2 }], + }); + expect(container).not.toHaveStyle({ transform: [{ rotate: '45deg' }] }); +}); + +test('handles when the style prop is undefined', () => { + const { getByTestId } = render( + + Hello World + + ); + + const container = getByTestId('container'); + + expect(container).not.toHaveStyle({ fontWeight: 'bold' }); +}); + +test('handles transform when transform undefined', () => { + const { getByTestId } = render( + + Hello World + + ); + + const container = getByTestId('container'); + expect(() => + expect(container).toHaveStyle({ transform: [{ scale: 1 }] }) + ).toThrowErrorMatchingSnapshot(); +}); + +test('handles Pressable with function style prop', () => { + const { getByTestId } = render( + ({ backgroundColor: 'blue' })} /> + ); + expect(getByTestId('test')).toHaveStyle({ backgroundColor: 'blue' }); +}); diff --git a/src/matchers/extend-expect.d.ts b/src/matchers/extend-expect.d.ts index acdb16d1d..a95174c5a 100644 --- a/src/matchers/extend-expect.d.ts +++ b/src/matchers/extend-expect.d.ts @@ -12,6 +12,7 @@ export interface JestNativeMatchers { toBeVisible(): R; toHaveDisplayValue(expectedValue: TextMatch, options?: TextMatchOptions): R; toHaveProp(name: string, expectedValue?: unknown): R; + toHaveStyle(style: StyleProp