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 tests #11

Merged
merged 7 commits into from
Jul 30, 2024
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
1,089 changes: 1,089 additions & 0 deletions __tests__/IgniteProvider.test.tsx

Large diffs are not rendered by default.

45 changes: 45 additions & 0 deletions __tests__/TicketsSdkEmbeddedIos.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import React from 'react';
import { TicketsSdkEmbeddedIos } from '../src/TicketsSdkEmbeddedIos';
import { render, waitFor } from '@testing-library/react-native';
import { Platform } from 'react-native';

jest.mock('react-native', () => {
const RN = jest.requireActual('react-native');
RN.requireNativeComponent = jest.fn();
return RN;
});

describe('TicketsSdkEmbeddedIos', () => {
beforeAll(() => {
Platform.OS = 'ios';
});

it('renders the TicketsSdk component after the specified delay', async () => {
const renderTimeDelay = 500;
const { queryByTestId, getByTestId } = render(
<TicketsSdkEmbeddedIos
style={{ flex: 1 }}
renderTimeDelay={renderTimeDelay}
/>
);

expect(queryByTestId('TicketsSdk')).toBeNull();

await waitFor(
() => {
expect(getByTestId('TicketsSdk')).toBeTruthy();
},
{ timeout: renderTimeDelay + 100 }
);
});

it('renders the TicketsSdk component immediately if no delay is specified', async () => {
const { getByTestId } = render(
<TicketsSdkEmbeddedIos style={{ flex: 1 }} />
);

await waitFor(() => {
expect(getByTestId('TicketsSdk')).toBeTruthy();
});
});
});
29 changes: 29 additions & 0 deletions __tests__/TicketsSdkModal.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react';
import { render } from '@testing-library/react-native';
import { TicketsSdkModal } from '../src/TicketsSdkModal';

const mockSetShowTicketsModal = jest.fn();

describe('<TicketsSdkModal />', () => {
it('renders TicketsSdkModal when showTicketsModal is true', async () => {
const { getByTestId } = render(
<TicketsSdkModal
showTicketsModal={true}
setShowTicketsModal={mockSetShowTicketsModal}
/>
);

expect(() => getByTestId('ticketsSDKWrapper')).not.toThrow();
});

it('does not render TicketsSdkModal when showTicketsModal is false', async () => {
const { getByTestId } = render(
<TicketsSdkModal
showTicketsModal={false}
setShowTicketsModal={mockSetShowTicketsModal}
/>
);

expect(() => getByTestId('ticketsSDKWrapper')).toThrow();
});
});
5 changes: 5 additions & 0 deletions example/__mocks__/react-native-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
DEMO_VENUE_ID: 'testVenueID',
DEMO_ATTRACTION_ID: 'testAttractionID',
DEMO_EVENT_ID: 'testEventID',
};
40 changes: 40 additions & 0 deletions example/__mocks__/react-native-ticketmaster-ignite.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { View, Text } from 'react-native';
import React from 'react';

export const useIgnite = jest.fn(() => ({
logout: jest.fn(),
login: jest.fn(),
getToken: jest.fn(),
getIsLoggedIn: jest.fn(),
isLoggingIn: false,
}));

export const RetailSDK = {
presentPurchase: jest.fn(),
presentPrePurchaseVenue: jest.fn(),
presentPrePurchaseAttraction: jest.fn(),
};

export const TicketsSdkEmbeddedIos = jest.fn(() => {
return (
<View>
<Text>Hello Test</Text>
</View>
);
});

export const TicketsSdkEmbeddedAndroid = jest.fn(() => {
return (
<View>
<Text>Hello Android</Text>
</View>
);
});

export const TicketsSdkModal = jest.fn(() => {
return (
<View>
<Text>Hello Modal</Text>
</View>
);
});
79 changes: 79 additions & 0 deletions example/__tests__/components/AccountsSDKOptions.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import React from 'react';
import { render, fireEvent } from '@testing-library/react-native';
import AccountsSDKOptions from '../../src/components/AccountsSDKOptions';
import { useIgnite } from 'react-native-ticketmaster-ignite';

describe('AccountsSDKOptions', () => {
const loginMock = jest.fn();
const logoutMock = jest.fn();
const getTokenMock = jest.fn();
const refreshTokenMock = jest.fn();
const getMemberInfoMock = jest.fn();
const getIsLoggedInMock = jest.fn();

beforeAll(() => {
jest.clearAllMocks();

// @ts-ignore
useIgnite.mockReturnValue({
login: loginMock,
logout: logoutMock,
getToken: getTokenMock,
refreshToken: refreshTokenMock,
getMemberInfo: getMemberInfoMock,
getIsLoggedIn: getIsLoggedInMock,
});
});

describe('AccountsSDKOptions', () => {
describe('when button is clicked, calls the library function', () => {
it('calls login func for Login button', () => {
const { getByText } = render(<AccountsSDKOptions />);

fireEvent(getByText('Login'), 'press');

expect(loginMock).toHaveBeenCalled();
});

it('calls logout func for Logout button', () => {
const { getByText } = render(<AccountsSDKOptions />);

fireEvent(getByText('Logout'), 'press');

expect(logoutMock).toHaveBeenCalled();
});

it('calls getToken func for Get Token button', () => {
const { getByText } = render(<AccountsSDKOptions />);

fireEvent(getByText('Get Token'), 'press');

expect(getTokenMock).toHaveBeenCalled();
});

it('calls refreshToken func for Refresh Token button', () => {
const { getByText } = render(<AccountsSDKOptions />);

fireEvent(getByText('Refresh Token'), 'press');

expect(refreshTokenMock).toHaveBeenCalled();
});

it('calls getMemberInfo func for Get Member button', () => {
const { getByText } = render(<AccountsSDKOptions />);

fireEvent(getByText('Get Member'), 'press');

expect(getMemberInfoMock).toHaveBeenCalled();
});

it('calls getIsLoggedIn for isLoggedIn button', () => {
const { getByText } = render(<AccountsSDKOptions />);

fireEvent(getByText('IsLoggedIn'), 'press');

expect(getIsLoggedInMock).toHaveBeenCalled();
});
});
});
});
36 changes: 36 additions & 0 deletions example/__tests__/components/RetailSDKOptions.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React from 'react';
import { render, fireEvent } from '@testing-library/react-native';
import RetailSDKOptions from '../../src/components/RetailSDKOptions';
import { RetailSDK } from 'react-native-ticketmaster-ignite';

describe('RetailSDKOptions', () => {
it('calls presentPrePurchaseVenue with venue ID when Show Retail PrePurchase Venue is clicked', () => {
const fakePresentPrePurchaseVenue = RetailSDK.presentPrePurchaseVenue;
const { getByText } = render(<RetailSDKOptions />);

fireEvent(getByText('Show Retail PrePurchase Venue'), 'press');

expect(fakePresentPrePurchaseVenue).toHaveBeenCalledWith('testVenueID');
});

it('calls presentPrePurchaseAttraction with attraction ID when Show Retail PrePurchase Attraction is clicked', () => {
const fakePresentPrePurchaseAttraction =
RetailSDK.presentPrePurchaseAttraction;
const { getByText } = render(<RetailSDKOptions />);

fireEvent(getByText('Show Retail PrePurchase Attraction'), 'press');

expect(fakePresentPrePurchaseAttraction).toHaveBeenCalledWith(
'testAttractionID'
);
});

it('calls presentPurchase with event ID when Show Retail Purchase is clicked', () => {
const fakePresentPurchase = RetailSDK.presentPurchase;
const { getByText } = render(<RetailSDKOptions />);

fireEvent(getByText('Show Retail Purchase'), 'press');

expect(fakePresentPurchase).toHaveBeenCalledWith('testEventID');
});
});
32 changes: 32 additions & 0 deletions example/__tests__/components/TicketsSDKOptions.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from 'react';
import { render, fireEvent } from '@testing-library/react-native';
import TicketsSDKOptions from '../../src/components/TicketsSDKOptions';
import { Platform } from 'react-native';

describe('TicketsSDKOptions', () => {
it('does not show the mobile option on android', () => {
Platform.OS = 'android';

const { getByText } = render(<TicketsSDKOptions />);

expect(() => getByText('Tickets SDK (Modal)')).toThrow();
});

it('shows the mobile option on ios', () => {
Platform.OS = 'ios';

const { getByText } = render(<TicketsSDKOptions />);

expect(() => getByText('Tickets SDK (Modal)')).not.toThrow();
});

it('when the modal button is clicked, render the modal from the library', () => {
Platform.OS = 'ios';

const { getByText } = render(<TicketsSDKOptions />);

fireEvent(getByText('Tickets SDK (Modal)'), 'press');

expect(getByText('Hello Modal')).toBeTruthy();
});
});
60 changes: 60 additions & 0 deletions example/__tests__/screens/Home.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import React from 'react';
import { render, waitFor } from '@testing-library/react-native';
import Home from '../../src/screens/Home';
import { useIgnite } from 'react-native-ticketmaster-ignite';
import { ActivityIndicator } from 'react-native';

describe('Home', () => {
const loginMock = jest.fn();
const logoutMock = jest.fn();
const getTokenMock = jest.fn();
const refreshTokenMock = jest.fn();
const getMemberInfoMock = jest.fn();
const getIsLoggedInMock = jest.fn();

beforeAll(() => {
jest.clearAllMocks();

// @ts-ignore
useIgnite.mockReturnValue({
login: loginMock,
logout: logoutMock,
getToken: getTokenMock,
refreshToken: refreshTokenMock,
getMemberInfo: getMemberInfoMock,
getIsLoggedIn: getIsLoggedInMock,
});
});

describe('Accounts SDK', () => {
describe('uses isLogging status to show spinner', () => {
it('shows the ActivityIndicator when isLogging in is true', async () => {
// @ts-ignore
useIgnite.mockReturnValue({
isLoggingIn: true,
});

const component = render(<Home />);

await waitFor(() => {
const spinner = component.root.findByType(ActivityIndicator);

expect(spinner).toBeTruthy();
});
});

it('does not show the ActivityIndicator when isLogging in is false', async () => {
// @ts-ignore
useIgnite.mockReturnValue({
isLoggingIn: false,
});

const component = render(<Home />);

await waitFor(() => {
expect(() => component.root.findByType(ActivityIndicator)).toThrow();
});
});
});
});
});
3 changes: 0 additions & 3 deletions example/jest.config.js

This file was deleted.

14 changes: 14 additions & 0 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.0.1",
"private": true,
"scripts": {
"test": "jest",
"android": "react-native run-android",
"ios": "react-native run-ios",
"start": "react-native start",
Expand All @@ -26,6 +27,9 @@
"@babel/runtime": "^7.20.0",
"@react-native/eslint-config": "^0.72.2",
"@react-native/metro-config": "^0.72.11",
"@testing-library/jest-native": "^5.4.3",
"@testing-library/react-hooks": "^8.0.1",
"@testing-library/react-native": "^11",
"@tsconfig/react-native": "^3.0.0",
"@types/react": "^18.0.24",
"@types/react-test-renderer": "^18.0.0",
Expand All @@ -39,5 +43,15 @@
},
"engines": {
"node": ">=18"
},
"jest": {
"preset": "react-native",
"modulePathIgnorePatterns": [
"<rootDir>/example/node_modules",
"<rootDir>/lib/"
],
"transformIgnorePatterns": [
"node_modules/(?!(jest-)?@?react-native|@react-native|@react-navigation)"
]
}
}
Loading
Loading