Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more component tests #323

Merged
merged 6 commits into from
Nov 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions components/ErrorView.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,39 @@ const ErrorView = ({
}}>
<View style={styles.body}>
<Icon
testID='error-view-icon'
name={icon.name}
type={icon.type}
size={isCompact(window) ? 60 : 100}
/>
<Text h2 style={{ ...styles.heading, marginVertical }}>{heading}</Text>
<Text style={{ ...styles.message, marginBottom: marginVertical }}>{message}</Text>
<Text
testID='error-view-heading'
h2
style={{ ...styles.heading, marginVertical }}
>
{heading}
</Text>
<Text
testID='error-view-message'
style={{ ...styles.message, marginBottom: marginVertical }}
>
{message}
</Text>
</View>
<View>
<View testID='error-view-details'>
{details.map((detailText, index) => (
<Text key={`errorview-details-${index}`} style={styles.details}>{detailText}</Text>
<Text
testID='error-view-detail'
key={`errorview-details-${index}`}
style={styles.details}
>
{detailText}
</Text>
))}
</View>
{buttonTitle && (
<Button
testID='error-view-button'
containerStyle={styles.footer}
icon={buttonIcon}
title={buttonTitle}
Expand Down
1 change: 1 addition & 0 deletions components/SwitchListItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const SwitchListItem = ({ item, index }) => (
{(
item.badge &&
<Badge
testID='badge'
value={item.badge.value}
status={item.badge.status}
containerStyle={{ marginStart: 8 }}
Expand Down
68 changes: 68 additions & 0 deletions components/__tests__/ErrorView.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { fireEvent, render } from '@testing-library/react-native';
import React from 'react';

import ErrorView from '../ErrorView';

describe('ErrorView', () => {
it('should render correctly', () => {
const { getByTestId, queryByTestId } = render(
<ErrorView
heading='Error Heading'
message='Error Message'
/>
);

expect(getByTestId('error-view-heading')).toHaveTextContent('Error Heading');
expect(getByTestId('error-view-message')).toHaveTextContent('Error Message');
expect(getByTestId('error-view-details')).toBeEmpty();
expect(queryByTestId('error-view-icon')).not.toBeNull();
expect(queryByTestId('error-view-button')).toBeNull();
});

it('should render button and handle presses', () => {
const onPress = jest.fn();

const { getByTestId } = render(
<ErrorView
heading='Error Heading'
message='Error Message'
buttonTitle='Test Button'
onPress={onPress}
/>
);

const button = getByTestId('error-view-button');
expect(button).toHaveTextContent('Test Button');
expect(onPress).not.toHaveBeenCalled();
fireEvent.press(button);
expect(onPress).toHaveBeenCalled();
});

it('should render details', () => {
const { getAllByTestId, getByTestId } = render(
<ErrorView
heading='Error Heading'
message='Error Message'
details={[
'Detail 0',
'Detail 1',
'Detail 2'
]}
/>
);

expect(getByTestId('error-view-details')).not.toBeEmpty();

const details = getAllByTestId('error-view-detail');
expect(details).toHaveLength(3);
expect(details[0]).toHaveTextContent('Detail 0');
expect(details[1]).toHaveTextContent('Detail 1');
expect(details[2]).toHaveTextContent('Detail 2');
});
});
24 changes: 24 additions & 0 deletions components/__tests__/RefreshWebView.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { render } from '@testing-library/react-native';
import React from 'react';

import RefreshWebView from '../RefreshWebView';

// NOTE: This test just verifies the component renders, because the
// functionality would be very difficult to test properly
describe('RefreshWebView', () => {
it('should render', () => {
const { toJSON } = render(
<RefreshWebView
isRefreshing={false}
/>
);

expect(toJSON()).toMatchSnapshot();
});
});
25 changes: 20 additions & 5 deletions components/__tests__/SwitchListItem.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('SwitchListItem', () => {
it('should render correctly and handle change event', () => {
const onValueChange = jest.fn();

const { getByTestId } = render(
const { getByTestId, queryByTestId } = render(
<SwitchListItem
index={0}
item={{
Expand All @@ -25,6 +25,8 @@ describe('SwitchListItem', () => {
);

expect(getByTestId('title')).toHaveTextContent('Test Switch');
expect(queryByTestId('subtitle')).toBeNull();
expect(queryByTestId('badge')).toBeNull();

const switchItem = getByTestId('switch');
expect(switchItem).toHaveProp('disabled', false);
Expand All @@ -35,16 +37,29 @@ describe('SwitchListItem', () => {
expect(onValueChange).toHaveBeenCalled();
});

it('should render a badge if provided', () => {
const { getByTestId } = render(
<SwitchListItem
index={0}
item={{
title: 'Test Switch',
badge: {
value: 'Badge'
}
}}
/>
);

expect(getByTestId('badge')).toHaveTextContent('Badge');
});

it('should render subtitle if provided', () => {
const { getByTestId } = render(
<SwitchListItem
index={0}
item={{
title: 'Test Switch',
subtitle: 'Test Subtitle',
disabled: false,
value: true,
onValueChange: jest.fn()
subtitle: 'Test Subtitle'
}}
/>
);
Expand Down
25 changes: 25 additions & 0 deletions components/__tests__/ThemeSwitcher.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { render } from '@testing-library/react-native';
import React from 'react';
import { ThemeProvider } from 'react-native-elements';

import ThemeSwitcher from '../ThemeSwitcher';

// NOTE: This test just verifies the component renders, because the
// functionality would be very difficult to test properly
describe('ThemeSwitcher', () => {
it('should render', () => {
const { toJSON } = render(
<ThemeProvider>
<ThemeSwitcher />
</ThemeProvider>
);

expect(toJSON()).toMatchSnapshot();
});
});
73 changes: 73 additions & 0 deletions components/__tests__/__snapshots__/RefreshWebView.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`RefreshWebView should render 1`] = `
<RCTScrollView
collapsable={false}
onGestureHandlerEvent={[Function]}
onGestureHandlerStateChange={[Function]}
onLayout={[Function]}
refreshControl={
<RefreshControlMock
enabled={false}
refreshing={false}
/>
}
showsHorizontalScrollIndicator={false}
showsVerticalScrollIndicator={false}
style={
Object {
"flex": 1,
"height": "100%",
}
}
>
<RCTRefreshControl />
<View>
<View
style={
Array [
Object {
"flex": 1,
"overflow": "hidden",
},
undefined,
]
}
>
<RNCWebView
cacheEnabled={true}
injectedJavaScriptBeforeContentLoadedForMainFrameOnly={true}
injectedJavaScriptForMainFrameOnly={true}
javaScriptEnabled={true}
messagingEnabled={false}
onContentProcessDidTerminate={[Function]}
onHttpError={[Function]}
onLoadingError={[Function]}
onLoadingFinish={[Function]}
onLoadingProgress={[Function]}
onLoadingStart={[Function]}
onMessage={[Function]}
onScroll={[Function]}
onShouldStartLoadWithRequest={[Function]}
source={null}
style={
Array [
Object {
"flex": 1,
"overflow": "hidden",
},
Object {
"backgroundColor": "#ffffff",
},
Object {
"flex": 1,
"height": 1334,
},
]
}
useSharedProcessPool={true}
/>
</View>
</View>
</RCTScrollView>
`;
3 changes: 3 additions & 0 deletions components/__tests__/__snapshots__/ThemeSwitcher.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ThemeSwitcher should render 1`] = `null`;
14 changes: 14 additions & 0 deletions constants/__tests__/Links.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { QuickStartUrl } from '../Links';

// I don't understand why jest thinks this needs test coverage, but it does =/
describe('Links', () => {
it('should return the right url', () => {
expect(QuickStartUrl).toBe('https://jellyfin.org/docs/general/quick-start.html');
});
});