Skip to content

Commit

Permalink
test: encodersAndDecoders pages
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-logan committed Sep 4, 2024
1 parent 331cc8f commit 249894e
Show file tree
Hide file tree
Showing 17 changed files with 1,364 additions and 7 deletions.
17 changes: 17 additions & 0 deletions jest/setupFiles.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// include this line for mocking react-native-gesture-handler
import "react-native-gesture-handler/jestSetup";

import mockClipboard from "@react-native-clipboard/clipboard/jest/clipboard-mock.js";

// include this section and the NativeAnimatedHelper section for mocking react-native-reanimated
jest.mock("react-native-reanimated", () => {
const Reanimated = require("react-native-reanimated/mock");
Expand Down Expand Up @@ -60,3 +62,18 @@ jest.mock("react-native-responsive-screen", () => ({
width: jest.fn().mockImplementation(() => 10),
height: jest.fn().mockImplementation(() => 10),
}));

jest.mock("@react-native-clipboard/clipboard", () => mockClipboard);

jest.mock("react-native-base64", () => ({
decode: jest.fn(),
encode: jest.fn(),
}));

jest.mock("react-native-vector-icons/MaterialCommunityIcons", () => "Icon");
jest.mock("react-native-vector-icons/FontAwesome5", () => "Icon");
jest.mock("react-native-vector-icons/FontAwesome", () => "Icon");
jest.mock("react-native-vector-icons/MaterialIcons", () => "Icon");
jest.mock("react-native-vector-icons/Feather", () => "Icon");
jest.mock("react-native-vector-icons/AntDesign", () => "Icon");
jest.mock("react-native-vector-icons/Entypo", () => "Icon");
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ const mockNavigation = {
isFocused: jest.fn(),
} as unknown as NavigationType;

jest.mock("react-native-vector-icons/MaterialCommunityIcons", () => "Icon");

describe("PrivacyPolicesAndTerms", () => {
it("should render correctly", () => {
render(<PrivacyPolicesAndTerms navigation={mockNavigation} />);
Expand Down
233 changes: 233 additions & 0 deletions src/pages/encodersAndDecoders/Base64/__tests__/Base64.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
import Clipboard from "@react-native-clipboard/clipboard";
import { fireEvent, render, screen } from "@testing-library/react-native";
import base64 from "react-native-base64";

import Base64Page from "..";

describe("Base64", () => {
it("should render correctly", () => {
render(<Base64Page />);

const text = screen.getByText(/Apagar apos gerar ?/);

expect(text).toBeTruthy();
});

describe("Checkbox", () => {
it("should change the value of the checkbox cleanAfterGenerate", async () => {
render(<Base64Page />);

const checkbox = await screen.findByTestId("checkbox-cleanAfterGenerate");

expect(checkbox.props.accessibilityState.checked).toBe(false);

fireEvent(checkbox, "onValueChange", true);

expect(checkbox.props.accessibilityState.checked).toBe(true);
});

it("should change the value of the checkbox considerSpace", async () => {
render(<Base64Page />);

const checkbox = await screen.findByTestId("checkbox-considerSpace");

expect(checkbox.props.accessibilityState.checked).toBe(false);

fireEvent(checkbox, "onValueChange", true);

expect(checkbox.props.accessibilityState.checked).toBe(true);
});
});

describe("TextInputs", () => {
describe("TextInput to encode", () => {
it("should change the value of the text input", async () => {
render(<Base64Page />);

const textInput = await screen.findByTestId("inputText");

expect(textInput.props.value).toBe("");

fireEvent.changeText(textInput, "test");

expect(textInput.props.value).toBe("test");
});
});

describe("TextInput to decode", () => {
it("should change the value of the text input", async () => {
render(<Base64Page />);

const textInput = await screen.findByTestId("inputBase64");

expect(textInput.props.value).toBe("");

fireEvent.changeText(textInput, "test");

expect(textInput.props.value).toBe("test");
});
});
});

describe("Buttons", () => {
describe("Button to encode", () => {
it("should encode the text", async () => {
jest.spyOn(base64, "encode").mockImplementation(() => "VGVzdGU=");

render(<Base64Page />);

const textInput = await screen.findByTestId("inputText");

fireEvent.changeText(textInput, "test");

const button = await screen.findByTestId("encode-buttonDispatch");

fireEvent.press(button);

const textInputBase64 = await screen.findByTestId("inputBase64");

expect(textInputBase64.props.value).toBe("VGVzdGU=");
});

it("should paste the encoded text in the clipboard", async () => {
const clipboardSpyOn = jest
.spyOn(Clipboard, "getString")
.mockResolvedValue("Teste");

render(<Base64Page />);

const button = await screen.findByTestId("encode-buttonPaste");

fireEvent.press(button);

const textInput = await screen.findByTestId("inputText");

expect(clipboardSpyOn).toHaveBeenCalled();

expect(textInput.props.value).toBe("Teste");
});

it("should copy the encoded text to the clipboard", async () => {
render(<Base64Page />);

const textInput = await screen.findByTestId("inputText");

fireEvent.changeText(textInput, "Teste");

const button = await screen.findByTestId("encode-buttonCopy");

fireEvent.press(button);

expect(textInput.props.value).toBe("Teste");
});

it("should cut the encoded text to the clipboard", async () => {
render(<Base64Page />);

const textInput = await screen.findByTestId("inputText");

fireEvent.changeText(textInput, "Teste");

const button = await screen.findByTestId("encode-buttonCut");

fireEvent.press(button);

expect(textInput.props.value).toBe("");
});

it("should clean the encoded text to the clipboard", async () => {
render(<Base64Page />);

const textInput = await screen.findByTestId("inputText");

fireEvent.changeText(textInput, "Teste");

const button = await screen.findByTestId("encode-buttonClean");

fireEvent.press(button);

expect(textInput.props.value).toBe("");
});
});

describe("Button to decode", () => {
it("should decode the text", async () => {
jest.spyOn(base64, "decode").mockImplementation(() => "test");

render(<Base64Page />);

const textInput = await screen.findByTestId("inputBase64");

fireEvent.changeText(textInput, "VGVzdGU=");

const button = await screen.findByTestId("decode-buttonDispatch");

fireEvent.press(button);

const textInputText = await screen.findByTestId("inputText");

expect(textInputText.props.value).toBe("test");
});

it("should paste the decoded text in the clipboard", async () => {
const clipboardSpyOn = jest
.spyOn(Clipboard, "getString")
.mockResolvedValue("Teste");

render(<Base64Page />);

const button = await screen.findByTestId("decode-buttonPaste");

fireEvent.press(button);

const textInput = await screen.findByTestId("inputBase64");

expect(clipboardSpyOn).toHaveBeenCalled();

expect(textInput.props.value).toBe("Teste");
});

it("should copy the decoded text to the clipboard", async () => {
render(<Base64Page />);

const textInput = await screen.findByTestId("inputBase64");

fireEvent.changeText(textInput, "Teste");

const button = await screen.findByTestId("decode-buttonCopy");

fireEvent.press(button);

expect(textInput.props.value).toBe("Teste");
});

it("should cut the decoded text to the clipboard", async () => {
render(<Base64Page />);

const textInput = await screen.findByTestId("inputBase64");

fireEvent.changeText(textInput, "Teste");

const button = await screen.findByTestId("decode-buttonCut");

fireEvent.press(button);

expect(textInput.props.value).toBe("");
});

it("should clean the decoded text to the clipboard", async () => {
render(<Base64Page />);

const textInput = await screen.findByTestId("inputBase64");

fireEvent.changeText(textInput, "Teste");

const button = await screen.findByTestId("decode-buttonClean");

fireEvent.press(button);

expect(textInput.props.value).toBe("");
});
});
});
});
14 changes: 14 additions & 0 deletions src/pages/encodersAndDecoders/Base64/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export default function Base64Page() {
{t("Apagar apos gerar ?")}
</Text>
<CheckBox
testID="checkbox-cleanAfterGenerate"
style={stylesWithTheme.checkbox}
value={cleanAlways}
onValueChange={async (cleanAlwaysChange) => {
Expand All @@ -141,6 +142,7 @@ export default function Base64Page() {
{t("Considerar espaço ?")}
</Text>
<CheckBox
testID="checkbox-considerSpace"
style={stylesWithTheme.checkbox}
value={considerSpace}
onValueChange={async (considerSpaceChange) => {
Expand All @@ -155,6 +157,7 @@ export default function Base64Page() {
<ScrollView showsVerticalScrollIndicator={false}>
<View style={stylesWithTheme.inputContainer}>
<TextInput
testID="inputText"
style={stylesWithTheme.input}
placeholder={t("Cole ou digite o texto aqui")}
placeholderTextColor={getThemeColor(theme, "placeHolderColor")}
Expand All @@ -164,24 +167,28 @@ export default function Base64Page() {
/>
<View style={stylesWithTheme.divButtonCopy}>
<TouchableOpacity
testID="encode-buttonPaste"
style={stylesWithTheme.buttonCopy}
onPress={() => pasteToClipboard("text")}
>
<FontAwesome name="paste" size={RFValue(26)} color="#007AFF" />
</TouchableOpacity>
<TouchableOpacity
testID="encode-buttonCopy"
style={stylesWithTheme.buttonCopy}
onPress={() => copyToClipboard(inputText)}
>
<FontAwesome name="copy" size={RFValue(26)} color="#007AFF" />
</TouchableOpacity>
<TouchableOpacity
testID="encode-buttonCut"
style={stylesWithTheme.buttonCopy}
onPress={() => cutToClipboard(inputText, "text")}
>
<FontAwesome name="cut" size={RFValue(26)} color="#007AFF" />
</TouchableOpacity>
<TouchableOpacity
testID="encode-buttonClean"
style={stylesWithTheme.buttonCopy}
onPress={() => cleanToClipboard("text")}
>
Expand All @@ -190,6 +197,7 @@ export default function Base64Page() {
</View>
</View>
<TouchableOpacity
testID="encode-buttonDispatch"
style={stylesWithTheme.button}
onPress={encodeToBase64}
>
Expand All @@ -199,6 +207,7 @@ export default function Base64Page() {
</TouchableOpacity>
<View style={stylesWithTheme.inputContainer}>
<TextInput
testID="inputBase64"
style={stylesWithTheme.input}
placeholder={t("Cole ou digite o código Base64 aqui")}
placeholderTextColor={getThemeColor(theme, "placeHolderColor")}
Expand All @@ -208,24 +217,28 @@ export default function Base64Page() {
/>
<View style={stylesWithTheme.divButtonCopy}>
<TouchableOpacity
testID="decode-buttonPaste"
style={stylesWithTheme.buttonCopy}
onPress={() => pasteToClipboard("base64")}
>
<FontAwesome name="paste" size={RFValue(26)} color="#007AFF" />
</TouchableOpacity>
<TouchableOpacity
testID="decode-buttonCopy"
style={stylesWithTheme.buttonCopy}
onPress={() => copyToClipboard(base64Text)}
>
<FontAwesome name="copy" size={RFValue(26)} color="#007AFF" />
</TouchableOpacity>
<TouchableOpacity
testID="decode-buttonCut"
style={stylesWithTheme.buttonCopy}
onPress={() => cutToClipboard(base64Text, "base64")}
>
<FontAwesome name="cut" size={RFValue(26)} color="#007AFF" />
</TouchableOpacity>
<TouchableOpacity
testID="decode-buttonClean"
style={stylesWithTheme.buttonCopy}
onPress={() => cleanToClipboard("base64")}
>
Expand All @@ -234,6 +247,7 @@ export default function Base64Page() {
</View>
</View>
<TouchableOpacity
testID="decode-buttonDispatch"
style={stylesWithTheme.button}
onPress={decodeFromBase64}
>
Expand Down
Loading

0 comments on commit 249894e

Please sign in to comment.