Best JavaScript code snippet using fast-check-monorepo
editor.js
Source:editor.js
1import * as actions from 'src/actions/old/editor';2import { timerInterval } from 'src/middlewares/editor/handlers';3import { DRAFT_REHYDRATE } from 'src/modules/old/Drafts/actions/actions';4import {5 EDITOR_REVERT_DRAFT,6 CLEAR_EPISODE_EDITION,7 ENABLE_TRIM,8 SHOW_DROP_WARNING,9} from 'src/modules/old/Editor/actions';10import { calculateEpisodeDuration } from 'src/modules/old/Editor/helpers';11import createReducer from './createReducer';12const timePerSection = {13 1: 120,14 2: 90,15 3: 60,16 4: 45,17 5: 40,18 6: 30,19 7: 20,20 8: 15,21 9: 10,22 10: 5,23};24const initialState = {25 currentTimelineTime: 0,26 pixelsPerSecond: 1,27 canPublish: true,28 secondsPerSection: 120,29 totalTimeSeconds: 2000, // total length of the timeline in seconds30 latestSnippetTime: 0,31 layers: [32 {33 audioVolume: 1,34 frontendId: 1,35 },36 ],37 layerRecordings: [],38 draftItems: [],39 isDropWarningVisible: false,40};41const handlers = {42 [SHOW_DROP_WARNING]: (state, action) => ({43 ...state,44 isDropWarningVisible: action.payload,45 }),46 [ENABLE_TRIM]: (state, action) => ({47 ...state,48 layerRecordings: state.layerRecordings.map(layer => {49 if (layer.frontendId !== action.payload.id) {50 return layer;51 }52 const width = state.pixelsPerSecond * layer.playDuration;53 const left = action.payload.position - 20;54 const right = action.payload.position + 20;55 return {56 ...layer,57 trim: {58 ...layer.trim,59 isTrimming: true,60 arrowLeft: left < 0 ? 0 : left,61 arrowRight: right > width ? width : right,62 },63 };64 }),65 }),66 [CLEAR_EPISODE_EDITION]: () => initialState,67 [actions.EDITOR_ADD_LAYER]: (state, payload) => {68 const newLayer = {69 name: `Layer ${payload.layerId}`,70 audioVolume: 1,71 frontendId: payload.layerId,72 };73 const layers = [newLayer, ...state.layers];74 return {75 ...state,76 layers,77 };78 },79 [actions.EDITOR_ADD_RECORDING_SNIPPET]: (state, { recordingSnippet }) => {80 const isAlreadyAd = state.layerRecordings.find(e => e.isAd);81 if (recordingSnippet.isAd && isAlreadyAd) {82 return state;83 }84 const newSnippets = [recordingSnippet, ...state.layerRecordings];85 return {86 ...state,87 layerRecordings: newSnippets,88 latestSnippetTime: calculateEpisodeDuration(newSnippets) || state.latestSnippetTime,89 };90 },91 [actions.EDITOR_ADD_AD_RECORDING_SNIPPET]: (state, { recordingSnippet }) => {92 const isAlreadyAd = state.layerRecordings.find(e => e.isAd);93 if (recordingSnippet.isAd && isAlreadyAd) {94 return state;95 }96 const newSnippets = [recordingSnippet, ...state.layerRecordings];97 return {98 ...state,99 layerRecordings: newSnippets,100 };101 },102 [actions.EDITOR_SWITCH_RECORDING_SNIPPET]: (state, { snippetId, newLayerId }) => {103 const updatedSnippets = state.layerRecordings.map(snippet => {104 if (snippet.frontendId === snippetId) {105 return {106 ...snippet,107 layer: newLayerId,108 };109 }110 return snippet;111 });112 return {113 ...state,114 layerRecordings: updatedSnippets,115 };116 },117 [actions.EDITOR_MOVE_RECORDING_SNIPPET]: (state, { recordingSnippet, newOffsetSeconds }) => {118 const updatedSnippets = state.layerRecordings.map(snippet => {119 if (snippet.frontendId !== recordingSnippet.frontendId) {120 return snippet;121 }122 return {123 ...snippet,124 timelineOffset: newOffsetSeconds,125 };126 });127 return {128 ...state,129 layerRecordings: updatedSnippets,130 };131 },132 [actions.EDITOR_DELETE_RECORDING_SNIPPET]: (state, { recordingSnippet }) => {133 const updatedSnippets = state.layerRecordings.filter(134 snippet => snippet.frontendId !== recordingSnippet.frontendId,135 );136 return {137 ...state,138 canPublish: true,139 latestSnippetTime: calculateEpisodeDuration(updatedSnippets),140 layerRecordings: updatedSnippets,141 };142 },143 [actions.EDITOR_FADE_RECORDING_SNIPPET]: (state, { frontendId, isFadeOut, fadeDuration }) => {144 const key = isFadeOut ? 'fadeOut' : 'fadeIn';145 const updatedSnippets = state.layerRecordings.map(snippet => {146 if (snippet.frontendId === frontendId) {147 return {148 ...snippet,149 fadeDuration,150 [key]: !snippet[key],151 };152 }153 return snippet;154 });155 return {156 ...state,157 layerRecordings: updatedSnippets,158 };159 },160 [actions.EDITOR_UPDATE_RECORDING_SNIPPET]: (state, { recordingSnippet }) => {161 const updatedSnippets = state.layerRecordings.map(snippet => {162 if (snippet.frontendId !== recordingSnippet.frontendId) {163 return snippet;164 }165 return recordingSnippet;166 });167 return {168 ...state,169 layerRecordings: updatedSnippets,170 };171 },172 [actions.EDITOR_INCREMENT_TIMELINE]: state => ({173 ...state,174 currentTimelineTime: state.currentTimelineTime + timerInterval,175 }),176 [actions.EDITOR_RESET]: () => ({177 ...initialState,178 }),179 [actions.EDITOR_SET_TIMELINE_TIME]: (state, action) => ({180 ...state,181 currentTimelineTime: action.newTime,182 }),183 [actions.EDITOR_SET_SNIPPET_DURATION]: (state, { snippet, newDuration }) => {184 const updatedSnippets = state.layerRecordings.map(recordingSnippet => {185 if (recordingSnippet.frontendId !== snippet.frontendId) {186 return recordingSnippet;187 }188 return {189 ...snippet,190 duration: newDuration,191 playDuration: Math.abs(newDuration / 1000 || snippet.playDuration),192 };193 });194 return {195 ...state,196 layerRecordings: updatedSnippets,197 };198 },199 [actions.EDITOR_RECOMPUTE_TIMELINE_LENGTH]: state => {200 const latestSnippetTime = state.layerRecordings.reduce((acc, val) => {201 const endTime = val.timelineOffset + val.playDuration;202 if (endTime > acc) {203 return endTime;204 }205 return acc;206 }, 0);207 const newTotalTime =208 latestSnippetTime + 120 > state.totalTimeSeconds ? latestSnippetTime + 140 : state.totalTimeSeconds;209 return {210 ...state,211 canPublish: true,212 latestSnippetTime,213 totalTimeSeconds: newTotalTime,214 };215 },216 [actions.EDITOR_SET_LAYER_VOLUME]: (state, { layerId, volume }) => {217 const updatedLayers = state.layers.map(layer => {218 if (layer.frontendId === layerId) {219 return {220 ...layer,221 audioVolume: volume,222 };223 }224 return layer;225 });226 return {227 ...state,228 layers: updatedLayers,229 };230 },231 [actions.EDITOR_CHANGE_ITEM_START_OFFSET]: (state, { snippet, deltaSeconds }) => {232 const updatedSnippets = state.layerRecordings.map(thisSnippet => {233 if (thisSnippet.frontendId === snippet.frontendId) {234 return {235 ...thisSnippet,236 startOffset: snippet.startOffset + deltaSeconds,237 playDuration: Math.abs(snippet.playDuration - deltaSeconds),238 };239 }240 return thisSnippet;241 });242 return {243 ...state,244 layerRecordings: updatedSnippets,245 };246 },247 [actions.EDITOR_CHANGE_ITEM_PLAY_DURATION]: (state, { snippet, deltaSeconds }) => {248 const updatedSnippets = state.layerRecordings.map(currentSnippet => {249 const thisSnippet = currentSnippet;250 if (thisSnippet.frontendId === snippet.frontendId) {251 const playDuration = snippet.playDuration + deltaSeconds;252 if (playDuration < 10) {253 delete thisSnippet.fadeIn;254 delete thisSnippet.fadeOut;255 }256 return {257 ...thisSnippet,258 playDuration,259 };260 }261 return thisSnippet;262 });263 return {264 ...state,265 canPublish: true,266 layerRecordings: updatedSnippets,267 };268 },269 [actions.EDITOR_SET_TRIM_MODE]: state => ({270 ...state,271 layerRecordings: state.layerRecordings.map(l => ({272 ...l,273 trim: { ...l.trim, isTrimming: false, arrowLeft: 0, arrowRight: 0 },274 })),275 }),276 [actions.EDITOR_SET_TRIM_X]: (state, { trim, snippet }) => {277 const updatedSnippets = state.layerRecordings.map(thisSnippet => {278 if (thisSnippet.frontendId !== snippet.frontendId) {279 return thisSnippet;280 }281 return {282 ...thisSnippet,283 trim,284 };285 });286 return {287 ...state,288 layerRecordings: updatedSnippets,289 };290 },291 [actions.EDITOR_SET_ZOOM]: (state, { pixelsPerSecond }) => {292 const updatedSnippets = state.layerRecordings.map(thisSnippet => {293 const previousWidth = state.pixelsPerSecond * thisSnippet.playDuration;294 const nextWidth = pixelsPerSecond * thisSnippet.playDuration;295 const scale = nextWidth / previousWidth;296 const arrowLeft = thisSnippet.trim.arrowLeft !== 0 ? thisSnippet.trim.arrowLeft * scale : 0;297 const arrowRight = thisSnippet.trim.arrowRight !== 0 ? thisSnippet.trim.arrowRight * scale : 0;298 return {299 ...thisSnippet,300 trim: {301 ...thisSnippet.trim,302 arrowLeft,303 arrowRight,304 },305 };306 });307 return {308 ...state,309 pixelsPerSecond,310 secondsPerSection: timePerSection[pixelsPerSecond],311 layerRecordings: updatedSnippets,312 };313 },314 [actions.EDITOR_TRIM_SELECTIONS]: state => {315 const updatedSnippets = state.layerRecordings316 .filter(snippet => snippet.playDuration !== 0)317 .map(thisSnippet => ({318 ...thisSnippet,319 trim: {320 arrowLeft: 0,321 arrowRight: 0,322 higher: null,323 },324 }));325 return {326 ...state,327 latestSnippetTime: calculateEpisodeDuration(updatedSnippets),328 layerRecordings: updatedSnippets,329 };330 },331 [DRAFT_REHYDRATE]: (_state, { draft }) => {332 const draftRehydrated = JSON.parse(draft.data);333 return {334 ...draftRehydrated,335 layers: draftRehydrated.layers.map(l => (Number.isInteger(l.audioVolume) ? l : { ...l, audioVolume: 1 })),336 layerRecordings: draftRehydrated.layerRecordings.map(l => ({337 ...l,338 trim: { arrowLeft: 0, arrowRight: 0, higher: null, isTrimming: false },339 })),340 };341 },342 [actions.EDITOR_INITIALIZE]: () => ({343 ...initialState,344 }),345 [EDITOR_REVERT_DRAFT]: () => ({346 ...initialState,347 }),348};...
Technology.jsx
Source:Technology.jsx
1import "../css/Technology.css";2import { v4 } from "uuid";3import {4 getFirestore,5 collection,6 getDocs,7 query,8 setDoc,9 doc,10 deleteDoc,11} from "firebase/firestore";12import { connectFirebase } from "../data/config";13import { useParams } from "react-router-dom";14import { useEffect, useState } from "react";15import AddSnippetForm from "../components/AddSnippetForm";16import Snippet from "../components/Snippet";17function Technology() {18 const [showForm, setShowForm] = useState(false);19 const [snippets, setSnippets] = useState([]);20 let params = useParams();21 connectFirebase();22 useEffect(() => {23 loadDatabase();24 }, []);25 const loadDatabase = async () => {26 const data = [];27 // Query our database for all documents in a specific collection28 const querySnapshot = await getDocs(29 query(collection(getFirestore(), `${params.technology}`))30 );31 // Traverse and add data to out component state32 querySnapshot.forEach((snippet) => {33 data.push(snippet.data());34 });35 setSnippets(data);36 };37 const toggleForm = () => {38 if (showForm) {39 setShowForm(false);40 } else {41 setShowForm(true);42 }43 };44 const handleAddSnippet = async (performsIn, snippetIn, languageIn) => {45 const generatedId = v4();46 try {47 await setDoc(doc(getFirestore(), params.technology, generatedId), {48 id: generatedId,49 language: languageIn,50 performs: performsIn,51 snippet: snippetIn,52 });53 } catch (error) {54 console.log(error);55 }56 const updatedSnippets = [...snippets];57 updatedSnippets.push({58 performs: performsIn,59 snippet: snippetIn,60 id: generatedId,61 });62 setSnippets(updatedSnippets);63 };64 const handleDeleteSnippet = async (id) => {65 const updatedSnippets = [...snippets];66 let indexToDelete = -1;67 updatedSnippets.forEach((snippet, i) => {68 if (snippet.id === id) {69 indexToDelete = i;70 }71 });72 updatedSnippets.splice(indexToDelete, 1);73 setSnippets(updatedSnippets);74 // Reflect deletion in database75 try {76 await deleteDoc(doc(getFirestore(), params.technology, id));77 } catch (error) {78 console.log(error);79 }80 };81 return (82 <div className="Technology pattern-grid-sm">83 <div className="page-title">84 <h2>85 {" "}86 {params.technology.substring(0, 1).toUpperCase() +87 params.technology.substring(1)}{" "}88 Collection89 </h2>90 <button onClick={() => toggleForm()} className="addTechFolder-btn">91 Add Snippet92 </button>93 </div>94 {/* Map through our snippets and return Snippet components with their relative data */}95 <div className="SnippetContainer">96 {snippets.map((curSnippet) => {97 return (98 <Snippet99 performs={curSnippet.performs}100 id={curSnippet.id}101 snippet={curSnippet.snippet}102 key={v4()}103 language={curSnippet.language}104 handleDeleteSnippet={handleDeleteSnippet}105 />106 );107 })}108 </div>109 {showForm ? (110 <AddSnippetForm111 technology={params.technology.toUpperCase()}112 toggleForm={toggleForm}113 handleAddSnippet={handleAddSnippet}114 />115 ) : null}116 </div>117 );118}...
Using AI Code Generation
1const fc = require('fast-check');2const { updatedSnippets } = require('fast-check/lib/types/property/Property.generic');3const { Property } = require('fast-check/lib/types/property/Property');4const { Shrinkable } = require('fast-check/lib/check/arbitrary/definition/Shrinkable');5const { ShrinkableArray } = require('fast-check/lib/check/arbitrary/definition/ShrinkableArray');6const { ShrinkableMap } = require('fast-check/lib/check/arbitrary/definition/ShrinkableMap');7const { ShrinkableSet } = require('fast-check/lib/check/arbitrary/definition/ShrinkableSet');8const { ShrinkableObject } = require('fast-check/lib/check/arbitrary/definition/ShrinkableObject');9const { ShrinkableString } = require('fast-check/lib/check/arbitrary/definition/ShrinkableString');10const { ShrinkableNumber } = require('fast-check/lib/check/arbitrary/definition/ShrinkableNumber');11const { ShrinkableBoolean } = require('fast-check/lib/check/arbitrary/definition/ShrinkableBoolean');12const { ShrinkableBigInt } = require('fast-check/lib/check/arbitrary/definition/ShrinkableBigInt');13const { ShrinkableUndefined } = require('fast-check/lib/check/arbitrary/definition/ShrinkableUndefined');14const { ShrinkableNull } = require('fast-check/lib/check/arbitrary/definition/ShrinkableNull');15const { ShrinkableDate } = require('fast-check/lib/check/arbitrary/definition/ShrinkableDate');16const { ShrinkableSymbol } = require('fast-check/lib/check/arbitrary/definition/ShrinkableSymbol');17const { ShrinkableFunction } = require('fast-check/lib/check/arbitrary/definition/ShrinkableFunction');18const { ShrinkableError } = require('fast-check/lib/check/arbitrary/definition/ShrinkableError');19const { ShrinkableRegExp } = require('fast-check/lib/check/arbitrary/definition/ShrinkableRegExp');20function stringifyShrinkable(shrinkable) {21 if (shrinkable instanceof Shrinkable) {22 if (shrinkable instanceof ShrinkableArray) {23 return `[${shrinkable.value_.map(stringifyShrinkable).join(', ')}]`;24 } else if (shrinkable instanceof ShrinkableMap) {25 return `{
Using AI Code Generation
1const { updatedSnippets } = require('fast-check');2const { snippets } = require('./test1');3const updatedSnippets = updatedSnippets(snippets);4console.log(updatedSnippets);5const { updatedSnippets } = require('fast-check-monorepo');6const { snippets } = require('./test1');7const updatedSnippets = updatedSnippets(snippets);8console.log(updatedSnippets);9const { updatedSnippets } = require('fast-check-monorepo');10const { snippets } = require('./test1');11const updatedSnippets2 = updatedSnippets(snippets);12console.log(updatedSnippets2);
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!