Skip to content

Commit 69fd1a2

Browse files
authored
feat: add story unit tests with compose story (storybookjs#551)
1 parent a285274 commit 69fd1a2

File tree

36 files changed

+9065
-8418
lines changed

36 files changed

+9065
-8418
lines changed

.circleci/config.yml

Lines changed: 0 additions & 65 deletions
This file was deleted.

.github/workflows/test.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Checks
2+
3+
on:
4+
push:
5+
branches:
6+
- next
7+
pull_request:
8+
types: [opened, synchronize, reopened]
9+
10+
jobs:
11+
build:
12+
name: Check everything!
13+
strategy:
14+
fail-fast: false
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v4
18+
- name: Set node version
19+
uses: actions/setup-node@v4
20+
with:
21+
node-version: 20
22+
- name: install and compile
23+
run: yarn
24+
- name: build
25+
run: yarn build
26+
- name: lint
27+
run: yarn lint
28+
- name: test
29+
run: yarn test

.yarn/releases/yarn-3.6.1.cjs

Lines changed: 0 additions & 874 deletions
This file was deleted.

.yarn/releases/yarn-4.1.0.cjs

Lines changed: 893 additions & 0 deletions
Large diffs are not rendered by default.

.yarnrc.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1-
yarnPath: .yarn/releases/yarn-3.6.1.cjs
2-
nodeLinker: node-modules
1+
compressionLevel: mixed
2+
3+
enableGlobalCache: false
4+
5+
nodeLinker: node-modules
6+
7+
yarnPath: .yarn/releases/yarn-4.1.0.cjs

examples/expo-example/App.test.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React from 'react';
2+
import renderer from 'react-test-renderer';
3+
4+
import App from './App';
5+
6+
describe('<App />', () => {
7+
it('has 1 child', () => {
8+
const tree = renderer.create(<App />).toJSON();
9+
if (!Array.isArray(tree) && tree.children) {
10+
expect(tree.children.length).toBe(1);
11+
} else {
12+
throw new Error('App has no children');
13+
}
14+
});
15+
});

examples/expo-example/App.tsx

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,3 @@
1-
// NOTE this fixes metro not logging correctly for some reason
2-
// if (__DEV__) {
3-
// const primitiveTypes = ['string', 'number', 'boolean'];
4-
// const logLevels = ['log', 'debug', 'info', 'warn', 'error'];
5-
6-
// const transformArgs = (args) => {
7-
// return args.map((arg) => {
8-
// if (arg === undefined) {
9-
// return 'undefined';
10-
// }
11-
// if (arg instanceof Error) {
12-
// if (arg.stack) {
13-
// return arg.stack;
14-
// }
15-
// return arg.toString();
16-
// }
17-
// if (arg instanceof Date) {
18-
// return arg.toString();
19-
// }
20-
// if (primitiveTypes.includes(typeof arg)) {
21-
// return arg.toString();
22-
// } else {
23-
// return JSON.stringify(arg);
24-
// }
25-
// });
26-
// };
27-
28-
// const consoleProxy = new Proxy(console, {
29-
// get: (target, prop) => {
30-
// //@ts-ignore
31-
// if (logLevels.includes(prop)) {
32-
// return (...args) => {
33-
// // we proxy the call to itself, but we transform the arguments to strings before
34-
// // so that they are printed in the terminal
35-
// return target[prop].apply(this, transformArgs(args));
36-
// };
37-
// }
38-
// return target[prop];
39-
// },
40-
// });
41-
42-
// console = consoleProxy;
43-
// }
441
import { Text, View } from 'react-native';
452
import Constants from 'expo-constants';
463

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { render, screen, userEvent } from '@testing-library/react-native';
2+
import { composeStory } from '@storybook/react';
3+
import Meta, { Basic } from './Actions.stories';
4+
5+
const ActionsStory = composeStory(Basic, Meta);
6+
7+
test('action story renders and onpress works', async () => {
8+
jest.useFakeTimers();
9+
10+
const onPress = jest.fn();
11+
12+
render(<ActionsStory onPress={onPress} />);
13+
14+
const user = userEvent.setup({});
15+
16+
const actionButton = screen.getByText('Press me!');
17+
18+
await user.press(actionButton);
19+
20+
expect(onPress).toHaveBeenCalled();
21+
});

examples/expo-example/components/BackgroundExample/BackgroundCsf.stories.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const styles = StyleSheet.create({
1111
text: { color: 'black' },
1212
});
1313

14-
const BackgroundMeta: Meta<typeof Background> = {
14+
const meta = {
1515
title: 'BackgroundExample/Background CSF',
1616
component: Background,
1717
decorators: [withBackgrounds],
@@ -26,10 +26,10 @@ const BackgroundMeta: Meta<typeof Background> = {
2626
],
2727
},
2828
},
29-
};
29+
} satisfies Meta<typeof Background>;
3030

31-
export default BackgroundMeta;
31+
export default meta;
3232

33-
type BackgroundStory = StoryObj<typeof Background>;
33+
type Story = StoryObj<typeof meta>;
3434

35-
export const Basic: BackgroundStory = {};
35+
export const Basic: Story = {};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import '@testing-library/react-native/extend-expect';
2+
import { render, screen } from '@testing-library/react-native';
3+
import { composeStory } from '@storybook/react';
4+
import Meta, { Basic } from './BackgroundCsf.stories';
5+
6+
const BackgroundCsfStory = composeStory(Basic, Meta);
7+
8+
test('Background colour is hotpink', () => {
9+
render(<BackgroundCsfStory />);
10+
11+
expect(screen.getByTestId('addon-backgrounds-container')).toHaveStyle({
12+
backgroundColor: 'hotpink',
13+
});
14+
});

examples/expo-example/components/ControlExamples/Array/Array.stories.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { StoryObj, Meta } from '@storybook/react';
22
import { Array } from './Array';
3-
const ArrayMeta: Meta<typeof Array> = {
3+
4+
const meta = {
45
title: 'ControlExamples/Array control',
56
component: Array,
67
args: {
@@ -16,10 +17,10 @@ const ArrayMeta: Meta<typeof Array> = {
1617
notes:
1718
'Seems like the array type is not distinguishable from object so you should provide the arg type in this case',
1819
},
19-
};
20+
} satisfies Meta<typeof Array>;
2021

21-
export default ArrayMeta;
22+
export default meta;
2223

23-
type ArrayStory = StoryObj<typeof Array>;
24+
type Story = StoryObj<typeof meta>;
2425

25-
export const Basic: ArrayStory = {};
26+
export const Basic: Story = {};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { render, screen } from '@testing-library/react-native';
2+
import { composeStory } from '@storybook/react';
3+
import Meta, { Basic } from './Array.stories';
4+
5+
const ArrayStory = composeStory(Basic, Meta);
6+
7+
test('array story renders', () => {
8+
render(<ArrayStory />);
9+
10+
expect(screen.getByTestId('array-story-container')).toHaveTextContent(/abc/);
11+
});

examples/expo-example/components/ControlExamples/Array/Array.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ interface ArrayProps {
66
}
77

88
export const Array = ({ list }: ArrayProps) => (
9-
<View>
9+
<View testID="array-story-container">
1010
{list.map((item, index) => (
1111
<Text key={index} style={styles.item}>
1212
{item}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { render, screen } from '@testing-library/react-native';
2+
import { composeStory } from '@storybook/react';
3+
import Meta, { Basic, On } from './Boolean.stories';
4+
5+
const BooleanStory = composeStory(Basic, Meta);
6+
const OnStory = composeStory(On, Meta);
7+
8+
test('boolean story renders', () => {
9+
render(<BooleanStory />);
10+
11+
screen.getByText('off');
12+
});
13+
14+
test('boolean story renders on', () => {
15+
render(<OnStory />);
16+
17+
screen.getByText('on');
18+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { render, screen } from '@testing-library/react-native';
2+
import { composeStory } from '@storybook/react';
3+
import Meta, { ColorExample } from './Color.stories';
4+
5+
const ColorStory = composeStory(ColorExample, Meta);
6+
7+
test('color story renders', () => {
8+
render(<ColorStory />);
9+
10+
expect(screen.getByTestId('color-story-container')).toHaveStyle({ backgroundColor: '#a819b9' });
11+
});

examples/expo-example/components/ControlExamples/Color/Color.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const styles = StyleSheet.create({
1010
});
1111

1212
export const Color = ({ color }: ButtonProps) => (
13-
<View style={[styles.button, { backgroundColor: color }]}>
13+
<View testID="color-story-container" style={[styles.button, { backgroundColor: color }]}>
1414
<Text>Color: {color as string}</Text>
1515
</View>
1616
);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { render, screen } from '@testing-library/react-native';
2+
import { composeStory } from '@storybook/react';
3+
import Meta, { Basic } from './Date.stories';
4+
5+
const DateStory = composeStory(Basic, Meta);
6+
7+
test('date story renders', () => {
8+
render(<DateStory />);
9+
10+
const date = new Date(1983, 1, 25);
11+
12+
screen.getByText(date.toString());
13+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { render, screen } from '@testing-library/react-native';
2+
import { composeStory } from '@storybook/react';
3+
import Meta, { Basic, Range } from './Number.stories';
4+
5+
const BasicStory = composeStory(Basic, Meta);
6+
const RangeStory = composeStory(Range, Meta);
7+
8+
test('basic story renders', async () => {
9+
render(<BasicStory />);
10+
11+
await screen.findByText(/5 x 3 = 15/);
12+
});
13+
14+
test('range story renders', async () => {
15+
render(<RangeStory />);
16+
17+
await screen.findByText(/6 x 7 = 42/);
18+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { render, screen } from '@testing-library/react-native';
2+
import { composeStory } from '@storybook/react';
3+
import Meta, { Basic } from './Object.stories';
4+
5+
const ObjectStory = composeStory(Basic, Meta);
6+
7+
test('object story renders', () => {
8+
render(<ObjectStory />);
9+
10+
screen.getByText('title: Blade Runner');
11+
screen.getByText('genre: Sci Fi');
12+
screen.getByText('release year: 1982');
13+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { render, screen } from '@testing-library/react-native';
2+
import { composeStory } from '@storybook/react';
3+
import Meta, { Basic } from './Radio.stories';
4+
5+
const RadioStory = composeStory(Basic, Meta);
6+
7+
test('radio story renders', () => {
8+
render(<RadioStory />);
9+
10+
screen.getByText('104.8MHz');
11+
});

examples/expo-example/components/ControlExamples/Reproductions/SelectWithNumber.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export const MyButton = ({ number }: ButtonProps) => {
2020
return (
2121
<View>
2222
<TouchableOpacity activeOpacity={0.8}>
23-
<Text>{number}</Text>
23+
<Text>num: {number}</Text>
2424
</TouchableOpacity>
2525
</View>
2626
);

0 commit comments

Comments
 (0)