UNPKG

@applicaster/quick-brick-core

Version:

Core package for Applicaster's Quick Brick App

160 lines (131 loc) 3.62 kB
import nock from "nock"; jest.mock("../../AssetCache", () => ({ cacheAssets: jest.fn((x) => x), })); const mockLoadAppContextData = jest.fn(() => ({})); jest.mock("@applicaster/zapp-react-native-redux", () => ({ ...jest.requireActual("@applicaster/zapp-react-native-redux"), loadAppContextData: mockLoadAppContextData, })); const uuid = "342ee911-03b6-4a16-adde-ece5ef739feb"; const { getRemoteContextData } = require("../getRemoteContextData"); const { localStorage: storage, } = require("@applicaster/zapp-react-native-bridge/ZappStorage/LocalStorage"); const storageGetSpy = jest.spyOn(storage, "getItem"); storageGetSpy.mockImplementation(() => Promise.resolve(uuid)); const layout = { screens: [ { id: "A123", name: "Home" }, { id: "B456", name: "River" }, ], content_types: { type: { screen_id: "A1234", }, }, }; const newThemeConfig = { app_background_color: "red", status_theme: "light-content", }; const pluginConfigurations = [ { plugin: { identifier: "foo", }, configuration_json: null, }, { plugin: { identifier: "theme", }, configuration_json: JSON.stringify(newThemeConfig), }, ]; const runtimeConfigurationUrls = { layout_url: "http://zapp.com/layout.json", plugin_configurations_url: "http://zapp.com/plugin_configurations.json", cell_styles_url: "http://zapp.com/cell_styles.json", presets_mapping_url: "http://zapp.com/presets_mapping.json", }; const plugins = [ { identifier: "dsp", configuration: null, }, { identifier: "theme", configuration: null, }, ]; const updatedPlugins = [ { identifier: "dsp", configuration: null, }, { identifier: "theme", configuration: newThemeConfig, }, ]; const cellStyles = { cell_id: { plugin_id: "", }, }; const presetsMapping = { version: 1, presets_mappings: { mappingSample: { id: "", name: "horizontal_list_qb", component_type: "horizontal_list_qb", }, }, }; const failingRuntimeConfigurationUrls = { ...runtimeConfigurationUrls, remote_configurations_url: "http://zapp.com/failing.json", }; const dispatch = jest.fn(); describe("getRemoteContextData", () => { beforeEach(() => { mockLoadAppContextData.mockClear(); nock("http://zapp.com").persist().get("/layout.json").reply(200, layout); nock("http://zapp.com") .get("/plugin_configurations.json") .reply(200, pluginConfigurations); nock("http://zapp.com").get("/cell_styles.json").reply(200, cellStyles); nock("http://zapp.com") .get("/presets_mapping.json") .reply(200, presetsMapping); nock("http://zapp.com") .get("/failing.json") .reply(Math.floor(Math.random() * 200 + 400), 0); }); it("returns a map of context data with the result of the matched urls", async () => { await getRemoteContextData(dispatch, runtimeConfigurationUrls, plugins); return expect(mockLoadAppContextData).toHaveBeenCalledWith( dispatch, expect.objectContaining({ pluginConfigurations, plugins: updatedPlugins, rivers: layout, contentTypes: layout.content_types, cellStyles, presetsMapping, }) ); }); it("skips the context data if it cannot be retrieved", async () => { expect(mockLoadAppContextData).not.toHaveBeenCalled(); return expect( getRemoteContextData(dispatch, failingRuntimeConfigurationUrls, plugins) ).rejects.toEqual( new Error( "cannot retrieve configuration data for remote_configurations_url" ) ); }); });