UNPKG

@applicaster/zapp-react-native-utils

Version:

Applicaster Zapp React Native utilities package

380 lines (302 loc) • 11 kB
import { createConfig } from "../createConfig"; const styles = { fields: [{ key: "field_1", initial_value: 1 }], label: "styles", }; const stylesWithGroup = { fields: [ { group: true, label: "testGroup", fields: [{ key: "field_1", initial_value: 1 }], }, ], }; const generators = { styles: jest.fn(() => ({ styles })), stylesGroup: jest.fn(() => ({ styles: stylesWithGroup, })), customConfiguration: jest.fn(() => ({ custom_configuration: [styles] })), customConfigurationGroup: jest.fn(() => ({ custom_configuration: [stylesWithGroup], })), }; jest.mock("../defaultManifestConfigurations", () => ({ defaultConfigurations: { styles: generators.styles, stylesGroup: generators.stylesGroup, customConfiguration: generators.customConfiguration, customConfigurationGroup: generators.customConfigurationGroup, }, })); describe("createConfig", () => { it("should return a function", () => { const currentResult = createConfig(jest.fn()); expect(typeof currentResult).toEqual("function"); }); it("should pass arguments to manifest factory", () => { const manifestFactory = jest.fn(); const props = { version: "9000", platform: "HAL" }; createConfig(manifestFactory)(props); expect(manifestFactory).toBeCalledWith(props); }); it("should return unmodified manifest if no extend key is provided", () => { const manifest = { identifier: "test" }; const manifestFactory = () => manifest; const currentValue = createConfig(manifestFactory)({}); expect(currentValue).toBe(manifest); }); it("should return unmodified manifest ig provided extend key doesn't exist", () => { const manifest = { identifier: "test" }; const manifestFactory = () => manifest; const currentValue = createConfig(manifestFactory, { extend: "non-existing-key", })({}); expect(currentValue).toBe(manifest); }); it("should pass the props to the manifest generator", () => { const manifest = { identifier: "test" }; const manifestFactory = () => manifest; const props = { version: "9000", platform: "HAL" }; createConfig(manifestFactory, { extend: "styles", })(props); expect(generators.styles).toBeCalledWith(props); }); it("should return base configuration if manifest is not provided", () => { const manifestFactory = () => undefined; const currentResult = createConfig(manifestFactory, { extend: "styles", })({}); expect(currentResult).toEqual({ styles }); }); describe("Data merging", () => { describe("Zapp configuration styles schema", () => { it("should add an extra style if key doesn't exist", () => { const newStyle = { key: "new-key" }; const manifestFactory = () => ({ styles: { fields: [newStyle] }, }); const currentResult = createConfig(manifestFactory, { extend: "styles", })({}); expect(currentResult.styles.fields).toEqual( expect.arrayContaining([newStyle]) ); }); it("should add new keys to the existing style key", () => { const newStyle = { key: "field_1", newKey: "newKey" }; const manifestFactory = () => ({ styles: { fields: [newStyle] }, }); const currentResult = createConfig(manifestFactory, { extend: "styles", })({}); expect(currentResult.styles.fields).toEqual( expect.arrayContaining([ expect.objectContaining({ newKey: "newKey" }), ]) ); }); it("should override default keys with the provided ones", () => { const newStyle = { key: "field_1", initial_value: "newValue" }; const manifestFactory = () => ({ styles: { fields: [newStyle] }, }); const currentResult = createConfig(manifestFactory, { extend: "styles", })({}); expect(currentResult.styles.fields).toEqual( expect.arrayContaining([newStyle]) ); }); }); describe("Zapp configuration styles schema with groups", () => { it("should add an extra style if key if doesn't exist", () => { const newStyle = { key: "new-key" }; const manifestFactory = () => ({ styles: { fields: [{ group: true, label: "testGroup", fields: [newStyle] }], }, }); const currentResult = createConfig(manifestFactory, { extend: "stylesGroup", })({}); expect(currentResult.styles.fields).toEqual( expect.arrayContaining([ expect.objectContaining({ fields: expect.arrayContaining([newStyle]), }), ]) ); }); it("should add new keys to the existing style key", () => { const newStyle = { key: "field_1", newKey: "newKey" }; const manifestFactory = () => ({ styles: { fields: [{ group: true, label: "testGroup", fields: [newStyle] }], }, }); const currentResult = createConfig(manifestFactory, { extend: "stylesGroup", })({}); expect(currentResult.styles.fields).toEqual( expect.arrayContaining([ expect.objectContaining({ fields: expect.arrayContaining([ expect.objectContaining({ newKey: "newKey" }), ]), }), ]) ); }); it("should override default keys with the provided ones", () => { const newStyle = { key: "field_1", initial_value: "newValue" }; const manifestFactory = () => ({ styles: { fields: [{ group: true, label: "testGroup", fields: [newStyle] }], }, }); const currentResult = createConfig(manifestFactory, { extend: "stylesGroup", })({}); expect(currentResult.styles.fields).toEqual( expect.arrayContaining([ expect.objectContaining({ fields: expect.arrayContaining([newStyle]), }), ]) ); }); }); describe("Zapp configuration custom_configuration schema", () => { it("should add an extra style if key doesn't exist", () => { const newStyle = { key: "new-key" }; const manifestFactory = () => ({ custom_configuration: [newStyle], }); const currentResult = createConfig(manifestFactory, { extend: "customConfiguration", })({}); expect(currentResult.custom_configuration).toEqual( expect.arrayContaining([newStyle]) ); }); it("should add new keys to the existing style key", () => { const newStyle = { key: "field_1", newKey: "newKey" }; const manifestFactory = () => ({ custom_configuration: [newStyle], }); const currentResult = createConfig(manifestFactory, { extend: "customConfiguration", })({}); expect(currentResult.custom_configuration).toEqual( expect.arrayContaining([ expect.objectContaining({ newKey: "newKey" }), ]) ); }); it("should override default keys with the provided ones", () => { const newStyle = { key: "field_1", initial_value: "newValue" }; const manifestFactory = () => ({ custom_configuration: [newStyle], }); const currentResult = createConfig(manifestFactory, { extend: "customConfiguration", })({}); expect(currentResult.custom_configuration).toEqual( expect.arrayContaining([newStyle]) ); }); }); describe("Zapp configuration custom_configuration schema with groups", () => { it("should add an extra style if key if doesn't exist", () => { const newStyle = { key: "new-key" }; const manifestFactory = () => ({ custom_configuration: [ { group: true, label: "testGroup", fields: [newStyle] }, ], }); const currentResult = createConfig(manifestFactory, { extend: "customConfigurationGroup", })({}); expect(currentResult.custom_configuration).toEqual( expect.arrayContaining([ expect.objectContaining({ fields: expect.arrayContaining([newStyle]), }), ]) ); }); it("should add new keys to the existing style key", () => { const newStyle = { key: "field_1", newKey: "newKey" }; const manifestFactory = () => ({ custom_configuration: [ { group: true, label: "testGroup", fields: [newStyle] }, ], }); const currentResult = createConfig(manifestFactory, { extend: "customConfigurationGroup", })({}); expect(currentResult.custom_configuration).toEqual( expect.arrayContaining([ expect.objectContaining({ fields: expect.arrayContaining([ expect.objectContaining({ newKey: "newKey" }), ]), }), ]) ); }); it("should override default keys with the provided ones", () => { const newStyle = { key: "field_1", initial_value: "newValue" }; const manifestFactory = () => ({ custom_configuration: [ { group: true, label: "testGroup", fields: [newStyle] }, ], }); const currentResult = createConfig(manifestFactory, { extend: "customConfigurationGroup", })({}); expect(currentResult.custom_configuration).toEqual( expect.arrayContaining([ expect.objectContaining({ fields: expect.arrayContaining([newStyle]), }), ]) ); }); }); describe("Zapp configuration root keys", () => { it("should add an extra style if key doesn't exist", () => { const newStyle = { key: "new-key" }; const manifestFactory = () => ({ newStyle, }); const currentResult = createConfig(manifestFactory, { extend: "styles", })({}); expect(currentResult.newStyle).toBe(newStyle); }); it("should add new keys to the existing style key", () => { const manifestFactory = () => ({ styles: { newKey: "newKey" }, }); const currentResult = createConfig(manifestFactory, { extend: "styles", })({}); expect(currentResult.styles.newKey).toEqual("newKey"); }); it("should override default keys with the provided ones", () => { const manifestFactory = () => ({ styles: { label: "newLabel" }, }); const currentResult = createConfig(manifestFactory, { extend: "styles", })({}); expect(currentResult.styles.label).toEqual("newLabel"); }); }); }); });