Skip to content

Commit

Permalink
Test for handleNewMedia method with Alert feature
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewC committed Sep 20, 2023
1 parent f0b68dd commit 8ac9081
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
31 changes: 30 additions & 1 deletion __tests__/AddNoteScreen.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,42 @@ import Adapter from 'enzyme-adapter-react-16';

Enzyme.configure({ adapter: new Adapter() });

import React from 'react';
import React, { SetStateAction, useState } from 'react';
import { TouchableOpacity, Alert } from 'react-native';
import { shallow } from "enzyme";
import { requestMediaLibraryPermissionsAsync, launchImageLibraryAsync } from "expo-image-picker";
import handleNewMedia from "../lib/components/photoScroller";

import AddNoteScreen from '../lib/screens/AddNoteScreen';
import PhotoScroller from "../lib/components/photoScroller";
import { Media } from '../lib/models/media_class';


describe("AddNoteScreen", () => {
it("renders without crashing", () => {
const wrapper = shallow(<AddNoteScreen />);
expect(wrapper).toMatchSnapshot();
});
});

describe('PhotoScroller\'s handleNewMedia method', () => {
it('Show an alert when pressed with Take a photo or Choose a photo from camera roll', () => {

const wrapper = shallow(<PhotoScroller newMedia={[]} setNewMedia={function (value: SetStateAction<Media[]>): void {
throw new Error('Function not implemented.');
} } active={true} />);
const button = wrapper.find('[testID="photoScrollerButton"]');

const mockAlert = jest.spyOn(Alert, 'alert');
button.props().onPress();

wrapper.find(TouchableOpacity).prop('onPress')();

expect(mockAlert).toHaveBeenCalledWith(
'Select Media',
'Choose the source for your media:',
expect.any(Array),
{ cancelable: false }
);
});
});
1 change: 1 addition & 0 deletions lib/components/photoScroller.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ const PhotoScroller = forwardRef(
{playing && renderImageView()}
<View style={{ flexDirection: "row" }}>
<TouchableOpacity
testID="photoScrollerButton"
style={[
PhotoStyles.image,
{
Expand Down
1 change: 1 addition & 0 deletions lib/screens/AddNoteScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ const AddNoteScreen: React.FC<AddNoteScreenProps> = ({ navigation, route }) => {
setIsLocation(false);
setIsTime(false);
}}
testID="images-icon"
>
<Ionicons name="images-outline" size={30} color="black" />
</TouchableOpacity>
Expand Down

0 comments on commit 8ac9081

Please sign in to comment.