@applicaster/zapp-react-native-utils
Version:
Applicaster Zapp React Native utilities package
262 lines (215 loc) • 6.25 kB
JavaScript
import * as R from "ramda";
import {
populateConfigurationValues,
imageSrcFromMediaItem,
getBoolFromConfigValue,
remapUpdatedKeys,
} from "../";
import { entry } from "./testEntry";
describe("getBoolFromConfigValue", () => {
it('returns true if value is "true" string', () => {
expect(getBoolFromConfigValue("true")).toEqual(true);
});
it('returns false if value is "false" string', () => {
expect(getBoolFromConfigValue("false")).toEqual(false);
});
it('returns true if value is "1" string', () => {
expect(getBoolFromConfigValue("1")).toEqual(true);
});
it('returns false if value is "0" string', () => {
expect(getBoolFromConfigValue("0")).toEqual(false);
});
it('returns true if value is "1" number', () => {
expect(getBoolFromConfigValue(1)).toEqual(true);
});
it('returns false if value is "0" number', () => {
expect(getBoolFromConfigValue(0)).toEqual(false);
});
it('returns true if value is "true" boolean', () => {
expect(getBoolFromConfigValue(true)).toEqual(true);
});
it('returns false if value is "false" boolean', () => {
expect(getBoolFromConfigValue(false)).toEqual(false);
});
it('returns false if value is "foo" wrong string', () => {
expect(getBoolFromConfigValue("foo")).toEqual(false);
});
it('returns false if value is "object"', () => {
expect(getBoolFromConfigValue({})).toEqual(false);
});
it('returns false if value is "null"', () => {
expect(getBoolFromConfigValue(null)).toEqual(false);
});
it('returns false if value is "undefined"', () => {
expect(getBoolFromConfigValue(undefined)).toEqual(false);
});
});
describe("imageSrcFromMediaItem", () => {
describe("returns the src value of first media_item", () => {
it("when the matching key is found and the src is not empty", () => {
const result = imageSrcFromMediaItem(entry, ["logo_thumbnail"]);
expect(result).toEqual(entry.media_group[1].media_item[0].src);
expect(result).not.toEqual("");
});
});
it("returns a media item with the 'image_base' key as a fallback", () => {
const result = imageSrcFromMediaItem(entry, ["does_not_exist"]);
const fallback = entry.media_group[0].media_item[0];
expect(result).toEqual(fallback.src);
expect(fallback.key).toBe("image_base");
});
it("returns undefined if the key was found but the source was empty", () => {
const badEntry = R.set(
R.lensPath(["media_group", 0, "media_item", 0, "src"]),
"",
entry
);
expect(imageSrcFromMediaItem(badEntry, ["image_base"])).toBeUndefined();
});
});
describe("populateConfigurationValues", () => {
it("transforms and returns the valid values", () => {
const fields = [
{
type: "switch",
key: "boolean_key",
initial_value: false,
},
{
type: "number_input",
key: "number_key",
initial_value: 30,
},
];
const configuration = {
boolean_key: "true",
number_key: "42",
};
const result = populateConfigurationValues(fields)(configuration);
expect(result).toHaveProperty("boolean_key", true);
expect(result).toHaveProperty("number_key", 42);
});
it("fallsback to default value of provided value is incorrect", () => {
const fields = [
{
type: "switch",
key: "boolean_key",
initial_value: true,
},
{
type: "number_input",
key: "number_key",
initial_value: 30,
},
];
const configuration = {
boolean_key: null,
number_key: null,
};
const result = populateConfigurationValues(fields)(configuration);
expect(result).toHaveProperty("boolean_key", true);
expect(result).toHaveProperty("number_key", 30);
});
it("allows number values to be 0", () => {
const fields = [
{
type: "number_input",
key: "number_key",
initial_value: 30,
},
];
const configuration = {
number_key: 0,
};
const result = populateConfigurationValues(fields)(configuration);
expect(result).toHaveProperty("number_key", 0);
});
it("uses default value if number input is sent as string", () => {
const fields = [
{
type: "number_input",
key: "number_key",
initial_value: 0,
},
];
const configuration = {
number_key: "not a number",
};
const result = populateConfigurationValues(fields)(configuration);
expect(result).toHaveProperty("number_key", 0);
});
it("uses default value if the value passed is an empty string", () => {
const fields = [
{
type: "number_input",
key: "number_key",
initial_value: null,
},
];
const configuration = {
number_key: "",
};
const result = populateConfigurationValues(fields)(configuration);
expect(result).toHaveProperty("number_key", null);
});
});
describe("remapUpdatedKeys", () => {
it("should return remapped config", () => {
const manifest = {
styles: {
updated_keys: {
old_key: "new_key",
},
},
};
const config = { styles: { old_key: "old_a" } };
expect(remapUpdatedKeys(manifest, config)).toMatchObject({
styles: {
new_key: "old_a",
},
});
});
it("should not update new keys", () => {
const manifest = {
styles: {
updated_keys: {
old_key: "new_key",
},
},
};
const config = {
styles: {
old_key: "old_a",
new_key: "old_b",
},
};
expect(remapUpdatedKeys(manifest, config)).toMatchObject({
styles: {
new_key: "old_b",
},
});
});
it("should retain other keys", () => {
const manifest = {
styles: {
updated_keys: {
old_key: "new_key",
},
},
};
const config = {
styles: {
some_key_a: "some_a",
some_key_b: "some_b",
some_key_c: "some_c",
},
};
expect(remapUpdatedKeys(manifest, config)).toMatchObject({
styles: {
some_key_a: "some_a",
some_key_b: "some_b",
some_key_c: "some_c",
},
});
});
});