@applicaster/zapp-react-native-utils
Version:
Applicaster Zapp React Native utilities package
106 lines (87 loc) • 2.69 kB
JavaScript
const { Platform } = require("react-native");
Platform.OS = "web";
import { directionHelper } from "../../directionHelper";
const globalTizen = {
tvinputdevice: {
registerKeyBatch: jest.fn(),
},
};
describe("keyCodes", () => {
describe("keyCode", () => {
const { keyCode } = require("..");
const { KEYS } = require("../keys.web");
it("returns an object for a given key event", () => {
expect(keyCode(KEYS.Enter)).toMatchSnapshot();
});
it("has a matcher method for a single key", () => {
expect(keyCode(KEYS.Enter).matches("Enter")).toBeTruthy();
expect(keyCode(KEYS.Enter).matches(KEYS.Enter)).toBeTruthy();
expect(keyCode(KEYS.Back).matches("Enter")).toBeFalsy();
});
it("has matcher for multiple keys", () => {
expect(
keyCode(KEYS.Enter).matchesAny("Enter", "Back", "ArrowUp")
).toBeTruthy();
expect(
keyCode(KEYS.Enter).matchesAny("Back", "ArrowUp", "ArrowDown")
).toBeFalsy();
});
it("returns the direction if it matches an arrow key", () => {
expect(keyCode(KEYS.Enter).direction()).toBe(null);
expect(keyCode(KEYS.ArrowDown).direction()).toEqual(
directionHelper(KEYS.ArrowDown)
);
});
describe("in web env", () => {
it("uses the proper keyCode for the environment", () => {
expect(keyCode({ keyCode: 13 }).matches(KEYS.Enter)).toBeTruthy();
});
});
});
describe("in Tizen Env", () => {
beforeEach(() => {
jest.resetModules();
global.tizen = globalTizen;
});
afterEach(() => {
global.tizen = undefined;
});
it("uses the proper keyCode for the environment", () => {
Platform.OS = "samsung_tv";
const { keyCode } = require("../");
const { KEYS } = require("../keys.web");
expect(globalTizen.tvinputdevice.registerKeyBatch).toHaveBeenCalledWith([
"MediaPlay",
"MediaPlayPause",
"MediaPause",
"MediaFastForward",
"MediaRewind",
"MediaStop",
"Exit",
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
]);
expect(keyCode({ keyCode: 10009 }).matches(KEYS.Back)).toBeTruthy();
});
});
describe("in WebOS Env", () => {
beforeEach(() => {
jest.resetModules();
});
it("uses the proper keyCode for the environment", () => {
Platform.OS = "lg_tv";
const { keyCode } = require("..");
const { KEYS } = require("../keys.web");
// Back is reserved on LG
expect(keyCode({ keyCode: 417 }).matches(KEYS.Forward)).toBeTruthy();
});
});
});