UNPKG

rnsst

Version:

React Native Storybook Screeshot Tests with Detox

47 lines (46 loc) 2.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.testStories = void 0; const channel_1 = require("./utils/channel"); const tests_1 = require("./utils/tests"); const fs = require("fs"); const path = require("path"); const DEFAULT_PORT = 7007; const testStories = async (config, beforeHandler) => { const { port, worker, totalWorkers } = config; const channel = new channel_1.default(port || DEFAULT_PORT); const stories = await (0, tests_1.getStories)(channel, beforeHandler); const storiesChunk = splitStoriesToChunks(stories, worker, totalWorkers); const testingLib = { testScreenshot: async (id) => { console.log('[rnsst] Handling ' + id); const file = await device.takeScreenshot(id); console.log('[rnsst] Saved to ' + file); if (config.captureDirectory) { const targetDir = path.resolve(process.cwd(), config.captureDirectory); fs.mkdirSync(targetDir, { recursive: true }); const savedFile = path.join(targetDir, `${id}.png`); fs.copyFile(file, savedFile, (err) => { console.log(err ? `[rnsst] Failed to copy output of ${id}: ` + err : '[rnsst] Copied to ' + savedFile); }); } }, }; await (0, tests_1.runTests)(testingLib, channel, storiesChunk); channel.stop(); }; exports.testStories = testStories; function splitStoriesToChunks(stories, worker, totalWorkers) { if (!worker || !totalWorkers) { return stories; } const arrayFromObject = Object.entries(stories).map(([key, value]) => ({ [key]: value, })); const size = Math.floor(arrayFromObject.length / totalWorkers); const leftOvers = arrayFromObject.length % totalWorkers; const getChunkStart = (index) => size * index + Math.min(leftOvers, index); const workerChunk = arrayFromObject.slice(getChunkStart(worker - 1), getChunkStart(worker)); const storiesChunk = workerChunk.reduce((acc, cur) => (Object.assign(Object.assign({}, acc), cur)), {}); return storiesChunk; }