Best JavaScript code snippet using root
ArtifactsManager.test.js
Source:ArtifactsManager.test.js
1const path = require('path');2const sleep = require('../utils/sleep');3const testSummaries = require('./templates/plugin/__mocks__/testSummaries.mock');4const testSuite = require('./templates/plugin/__mocks__/testSuite.mock');5describe('ArtifactsManager', () => {6 let proxy, FakePathBuilder;7 beforeEach(() => {8 jest.mock('fs-extra');9 jest.mock('./__mocks__/FakePathBuilder');10 jest.mock('./utils/ArtifactPathBuilder');11 jest.mock('../utils/argparse');12 jest.mock('../utils/logger');13 FakePathBuilder = require('./__mocks__/FakePathBuilder');14 proxy = {15 get ArtifactPathBuilder() {16 return require('./utils/ArtifactPathBuilder');17 },18 get ArtifactsManager() {19 return require('./ArtifactsManager');20 },21 get logger() {22 return require('../utils/logger');23 },24 get fs() {25 return require('fs-extra');26 },27 get argparse() {28 return require('../utils/argparse');29 },30 };31 });32 describe('when plugin factory is registered', () => {33 let artifactsManager, factory, plugin;34 beforeEach(() => {35 factory = jest.fn().mockReturnValue(plugin = {36 onBeforeLaunchApp: jest.fn(),37 });38 artifactsManager = new proxy.ArtifactsManager({39 pathBuilder: new proxy.ArtifactPathBuilder({40 rootDir: '/tmp',41 }),42 plugins: {43 mock: { setting: 'value' },44 },45 });46 artifactsManager.registerArtifactPlugins({ mock: factory });47 });48 it('should get called immediately', () => {49 expect(factory).toHaveBeenCalledWith(expect.objectContaining({50 userConfig: { setting: 'value' },51 preparePathForArtifact: expect.any(Function),52 trackArtifact: expect.any(Function),53 untrackArtifact: expect.any(Function),54 requestIdleCallback: expect.any(Function),55 }));56 });57 });58 describe('.artifactsApi', () => {59 let artifactsManager, artifactsApi;60 let testPluginFactory, testPlugin;61 let pathBuilder;62 beforeEach(async () => {63 testPlugin = null;64 testPluginFactory = (api) => {65 artifactsApi = api;66 return (testPlugin = {67 name: 'testPlugin',68 userConfig: api.userConfig,69 disable: jest.fn(),70 onBootDevice: jest.fn(),71 onBeforeShutdownDevice: jest.fn(),72 onShutdownDevice: jest.fn(),73 onBeforeUninstallApp: jest.fn(),74 onBeforeTerminateApp: jest.fn(),75 onTerminateApp: jest.fn(),76 onBeforeLaunchApp: jest.fn(),77 onLaunchApp: jest.fn(),78 onCreateExternalArtifact: jest.fn(),79 onTestStart: jest.fn(),80 onTestDone: jest.fn(),81 onSuiteStart: jest.fn(),82 onSuiteEnd: jest.fn(),83 onInit: jest.fn(),84 onBeforeCleanup: jest.fn(),85 });86 };87 pathBuilder = new FakePathBuilder();88 artifactsManager = new proxy.ArtifactsManager({89 pathBuilder,90 plugins: {91 testPlugin: {92 lifecycle: 'all',93 }94 }95 });96 artifactsManager.registerArtifactPlugins({ testPlugin: testPluginFactory });97 });98 describe('.userConfig', () => {99 it('should contain plugin config', async () => {100 expect(artifactsApi.userConfig).toEqual({ lifecycle: 'all' });101 });102 });103 describe('.preparePathForArtifact()', () => {104 let argparse, fs;105 beforeEach(() => {106 argparse = require('../utils/argparse');107 fs = require('fs-extra');108 });109 it('should prepare directory for test artifact at given path', async () => {110 const testSummary = {};111 const givenArtifactPath = path.join('artifacts', 'something', 'startup.log');112 pathBuilder.buildPathForTestArtifact.mockReturnValue(givenArtifactPath);113 const returnedArtifactPath = await artifactsApi.preparePathForArtifact('test', testSummary);114 expect(pathBuilder.buildPathForTestArtifact).toHaveBeenCalledWith('test', testSummary);115 expect(returnedArtifactPath).toBe(givenArtifactPath);116 expect(fs.ensureDir).toHaveBeenCalledWith(path.join('artifacts', 'something'));117 });118 });119 describe('.requestIdleCallback()', () => {120 let callbacks, resolves, rejects;121 beforeEach(() => {122 callbacks = new Array(3);123 resolves = new Array(3);124 rejects = new Array(3);125 for (const index of [0, 1, 2]) {126 const promise = new Promise((resolve, reject) => {127 resolves[index] = resolve;128 rejects[index] = reject;129 });130 callbacks[index] = jest.fn(() => promise);131 }132 });133 it('should enqueue an async operation to a queue that executes operations only one by one', async () => {134 artifactsApi.requestIdleCallback(callbacks[0], testPlugin);135 artifactsApi.requestIdleCallback(callbacks[1], testPlugin);136 expect(callbacks[0]).toHaveBeenCalledTimes(0);137 expect(callbacks[1]).toHaveBeenCalledTimes(0);138 await sleep(0);139 expect(callbacks[0]).toHaveBeenCalledTimes(1);140 expect(callbacks[1]).toHaveBeenCalledTimes(0);141 await (resolves[0](), sleep(0));142 expect(callbacks[1]).toHaveBeenCalledTimes(1);143 });144 it('should catch errors, report them if callback fails, and move on ', async () => {145 artifactsApi.requestIdleCallback(callbacks[0], testPlugin);146 artifactsApi.requestIdleCallback(callbacks[1], testPlugin);147 rejects[0](new Error('test onIdleCallback error'));148 resolves[1]();149 expect(callbacks[0]).not.toHaveBeenCalled();150 expect(callbacks[1]).not.toHaveBeenCalled();151 expect(proxy.logger.warn.mock.calls.length).toBe(0);152 await sleep(0);153 expect(callbacks[0]).toHaveBeenCalled();154 expect(callbacks[1]).toHaveBeenCalled();155 expect(proxy.logger.warn.mock.calls.length).toBe(1);156 });157 it('should work correctly for onBeforeCleanup', async () => {158 resolves[0](); resolves[1](); resolves[2]();159 artifactsApi.requestIdleCallback(callbacks[0], testPlugin);160 artifactsApi.requestIdleCallback(callbacks[1], testPlugin);161 artifactsApi.requestIdleCallback(callbacks[2], testPlugin);162 await artifactsManager.onBeforeCleanup();163 expect(callbacks[0]).toHaveBeenCalledTimes(1);164 expect(callbacks[1]).toHaveBeenCalledTimes(1);165 expect(callbacks[2]).toHaveBeenCalledTimes(1);166 });167 });168 describe('hooks', () => {169 describe('error handling', () => {170 function itShouldCatchErrorsOnPhase(hookName, argFactory) {171 it(`should catch .${hookName} errors`, async () => {172 testPlugin[hookName].mockImplementation(() => {173 throw new Error(`test ${hookName} error`);174 });175 await artifactsManager[hookName](argFactory());176 expect(proxy.logger.warn.mock.calls).toEqual([[177 {178 err: expect.any(Error),179 event: 'SUPPRESS_PLUGIN_ERROR',180 methodName: hookName,181 plugin: 'testPlugin',182 },183 expect.stringContaining(`Suppressed error inside function call: testPlugin.${hookName}`)184 ]]);185 });186 }187 itShouldCatchErrorsOnPhase('onTestStart', () => testSummaries.running());188 itShouldCatchErrorsOnPhase('onTestDone', () => testSummaries.passed());189 itShouldCatchErrorsOnPhase('onSuiteStart', () => (testSuite.mock()));190 itShouldCatchErrorsOnPhase('onSuiteEnd', () => (testSuite.mock()));191 itShouldCatchErrorsOnPhase('onInit', () => undefined);192 itShouldCatchErrorsOnPhase('onBeforeCleanup', () => undefined);193 itShouldCatchErrorsOnPhase('onBootDevice', () => ({194 coldBoot: false,195 deviceId: 'testDeviceId',196 }));197 itShouldCatchErrorsOnPhase('onCreateExternalArtifact', () => ({198 pluginId: 'testPlugin',199 artifactName: 'example',200 artifactPath: '/tmp/path/to/artifact',201 }));202 itShouldCatchErrorsOnPhase('onBeforeShutdownDevice', () => ({203 deviceId: 'testDeviceId'204 }));205 itShouldCatchErrorsOnPhase('onShutdownDevice', () => ({206 deviceId: 'testDeviceId'207 }));208 itShouldCatchErrorsOnPhase('onBeforeLaunchApp', () => ({209 bundleId: 'testBundleId',210 deviceId: 'testDeviceId',211 }));212 itShouldCatchErrorsOnPhase('onLaunchApp', () => ({213 bundleId: 'testBundleId',214 deviceId: 'testDeviceId',215 pid: 2018,216 }));217 itShouldCatchErrorsOnPhase('onBeforeTerminateApp', () => ({218 bundleId: 'testBundleId',219 deviceId: 'testDeviceId',220 }));221 itShouldCatchErrorsOnPhase('onTerminateApp', () => ({222 bundleId: 'testBundleId',223 deviceId: 'testDeviceId',224 }));225 itShouldCatchErrorsOnPhase('onBeforeUninstallApp', () => ({226 bundleId: 'testBundleId',227 deviceId: 'testDeviceId',228 }));229 });230 describe('onTestStart', () => {231 it('should call onTestStart in plugins with the passed argument', async () => {232 const testSummary = testSummaries.running();233 expect(testPlugin.onTestStart).not.toHaveBeenCalled();234 await artifactsManager.onTestStart(testSummary);235 expect(testPlugin.onTestStart).toHaveBeenCalledWith(testSummary);236 });237 });238 describe('onTestDone', () => {239 it('should call onTestDone in plugins with the passed argument', async () => {240 const testSummary = testSummaries.passed();241 expect(testPlugin.onTestDone).not.toHaveBeenCalled();242 await artifactsManager.onTestDone(testSummary);243 expect(testPlugin.onTestDone).toHaveBeenCalledWith(testSummary);244 });245 });246 describe('onSuiteStart', () => {247 it('should call onSuiteStart in plugins with the passed argument', async () => {248 const suite = testSuite.mock();249 expect(testPlugin.onSuiteStart).not.toHaveBeenCalled();250 await artifactsManager.onSuiteStart(suite);251 expect(testPlugin.onSuiteStart).toHaveBeenCalledWith(suite);252 });253 });254 describe('onSuiteEnd', () => {255 it('should call onSuiteEnd in plugins with the passed argument', async () => {256 const suite = testSuite.mock();257 expect(testPlugin.onSuiteEnd).not.toHaveBeenCalled();258 await artifactsManager.onSuiteEnd(suite);259 expect(testPlugin.onSuiteEnd).toHaveBeenCalledWith(suite);260 });261 });262 describe('onInit', () => {263 it('should call onInit in plugins', async () => {264 expect(testPlugin.onInit).not.toHaveBeenCalled();265 await artifactsManager.onInit();266 expect(testPlugin.onInit).toHaveBeenCalled();267 });268 });269 describe('onBeforeCleanup', () => {270 it('should call onBeforeCleanup in plugins', async () => {271 expect(testPlugin.onBeforeCleanup).not.toHaveBeenCalled();272 await artifactsManager.onBeforeCleanup();273 expect(testPlugin.onBeforeCleanup).toHaveBeenCalled();274 });275 });276 describe('onBootDevice', () => {277 it('should call onBootDevice in plugins', async () => {278 const bootInfo = {279 coldBoot: false,280 deviceId: 'testDeviceId',281 };282 expect(testPlugin.onBootDevice).not.toHaveBeenCalled();283 await artifactsManager.onBootDevice(bootInfo);284 expect(testPlugin.onBootDevice).toHaveBeenCalledWith(bootInfo);285 });286 });287 describe('onBeforeTerminateApp', () => {288 it('should call onBeforeTerminateApp in plugins', async () => {289 const terminateInfo = {290 deviceId: 'testDeviceId',291 bundleId: 'testBundleId',292 };293 expect(testPlugin.onBeforeTerminateApp).not.toHaveBeenCalled();294 await artifactsManager.onBeforeTerminateApp(terminateInfo);295 expect(testPlugin.onBeforeTerminateApp).toHaveBeenCalledWith(terminateInfo);296 });297 });298 describe('onTerminateApp', () => {299 it('should call onTerminateApp in plugins', async () => {300 const terminateInfo = {301 deviceId: 'testDeviceId',302 bundleId: 'testBundleId',303 };304 expect(testPlugin.onBeforeTerminateApp).not.toHaveBeenCalled();305 await artifactsManager.onBeforeTerminateApp(terminateInfo);306 expect(testPlugin.onBeforeTerminateApp).toHaveBeenCalledWith(terminateInfo);307 });308 });309 describe('onBeforeUninstallApp', () => {310 it('should call onBeforeUninstallApp in plugins', async () => {311 const uninstallInfo = {312 deviceId: 'testDeviceId',313 bundleId: 'testBundleId',314 };315 expect(testPlugin.onBeforeUninstallApp).not.toHaveBeenCalled();316 await artifactsManager.onBeforeUninstallApp(uninstallInfo);317 expect(testPlugin.onBeforeUninstallApp).toHaveBeenCalledWith(uninstallInfo);318 });319 });320 describe('onBeforeShutdownDevice', () => {321 it('should call onBeforeShutdownDevice in plugins', async () => {322 const shutdownInfo = {323 deviceId: 'testDeviceId',324 };325 expect(testPlugin.onBeforeShutdownDevice).not.toHaveBeenCalled();326 await artifactsManager.onBeforeShutdownDevice(shutdownInfo);327 expect(testPlugin.onBeforeShutdownDevice).toHaveBeenCalledWith(shutdownInfo);328 });329 });330 describe('onShutdownDevice', () => {331 it('should call onShutdownDevice in plugins', async () => {332 const shutdownInfo = {333 deviceId: 'testDeviceId',334 };335 expect(testPlugin.onShutdownDevice).not.toHaveBeenCalled();336 await artifactsManager.onShutdownDevice(shutdownInfo);337 expect(testPlugin.onShutdownDevice).toHaveBeenCalledWith(shutdownInfo);338 });339 });340 });341 describe('onCreateExternalArtifact', () => {342 it('should call onCreateExternalArtifact in a specific plugin', async () => {343 await artifactsManager.onCreateExternalArtifact({344 pluginId: 'testPlugin',345 artifactPath: '/tmp/path/to/artifact',346 artifactName: 'Example',347 });348 expect(testPlugin.onCreateExternalArtifact).toHaveBeenCalledWith({349 artifact: expect.any(Object),350 name: 'Example',351 });352 });353 });354 describe('onBeforeLaunchApp', () => {355 it('should call onBeforeLaunchApp in plugins', async () => {356 const launchInfo = {357 deviceId: 'testDeviceId',358 bundleId: 'testBundleId',359 };360 expect(testPlugin.onBeforeLaunchApp).not.toHaveBeenCalledWith(launchInfo);361 await artifactsManager.onBeforeLaunchApp(launchInfo);362 expect(testPlugin.onBeforeLaunchApp).toHaveBeenCalledWith(launchInfo);363 });364 });365 describe('onLaunchApp', () => {366 it('should call onLaunchApp in plugins', async () => {367 const launchInfo = {368 deviceId: 'testDeviceId',369 bundleId: 'testBundleId',370 pid: 2018,371 };372 await artifactsManager.onBeforeLaunchApp({ ...launchInfo, pid: NaN });373 expect(testPlugin.onLaunchApp).not.toHaveBeenCalled();374 await artifactsManager.onLaunchApp(launchInfo);375 expect(testPlugin.onLaunchApp).toHaveBeenCalledWith(launchInfo);376 });377 });378 });...
ArtifactsManager.js
Source:ArtifactsManager.js
...69 }70 async onBeforeTerminateApp(appInfo) {71 await this._callPlugins('plain', 'onBeforeTerminateApp', appInfo);72 }73 async onTerminateApp(appInfo) {74 await this._callPlugins('plain', 'onTerminateApp', appInfo);75 }76 async onBeforeUninstallApp(appInfo) {77 await this._callPlugins('plain', 'onBeforeUninstallApp', appInfo);78 }79 async onBeforeShutdownDevice(deviceInfo) {80 await this._callPlugins('plain', 'onBeforeShutdownDevice', deviceInfo);81 }82 async onShutdownDevice(deviceInfo) {83 await this._callPlugins('plain', 'onShutdownDevice', deviceInfo);84 }85 async onBeforeLaunchApp(appLaunchInfo) {86 await this._callPlugins('plain', 'onBeforeLaunchApp', appLaunchInfo);87 }...
Using AI Code Generation
1import {Navigation} from 'react-native-navigation';2import {registerScreens} from './screens';3Navigation.events().registerAppLaunchedListener(() => {4 Navigation.setRoot({5 root: {6 component: {7 }8 }9 });10});11Navigation.events().registerAppLaunchedListener(() => {12 Navigation.setDefaultOptions({13 topBar: {14 }15 });16});17Navigation.events().registerAppLaunchedListener(() => {18 Navigation.setDefaultOptions({19 topBar: {20 }21 });22});23Navigation.events().registerAppLaunchedListener(() => {24 Navigation.setRoot({25 root: {26 component: {27 }28 }29 });30});31Navigation.events().registerAppLaunchedListener(() => {32 Navigation.setRoot({33 root: {34 stack: {35 {36 component: {37 }38 }39 }40 }41 });42});43Navigation.events().registerAppLaunchedListener(() => {44 Navigation.setRoot({45 root: {46 stack: {47 {48 component: {49 }50 }51 }52 }53 });54});55Navigation.events().registerAppLaunchedListener(() => {56 Navigation.setRoot({57 root: {58 stack: {59 {60 component: {61 }62 }63 }64 }65 });66});67Navigation.events().registerAppLaunchedListener(() => {68 Navigation.setRoot({69 root: {70 stack: {71 {72 component: {73 }74 }75 }76 }77 });78});79Navigation.events().registerAppLaunchedListener(() => {80 Navigation.setRoot({81 root: {82 stack: {83 {84 component: {85 }86 }87 }88 }89 });90});91Navigation.events().registerAppLaunchedListener(() => {92 Navigation.setRoot({93 root: {
Using AI Code Generation
1import {Navigation} from 'react-native-navigation';2import {onTerminateApp} from './src/Navigation';3Navigation.events().registerAppLaunchedListener(() => {4 Navigation.setRoot({5 root: {6 component: {7 options: {8 topBar: {9 title: {10 },11 },12 },13 },14 },15 });16});17Navigation.events().registerAppLaunchedListener(() => {18 Navigation.setRoot({19 root: {20 stack: {21 {22 component: {23 options: {24 topBar: {25 title: {26 },27 },28 },29 },30 },31 },32 },33 });34});35Navigation.events().registerAppLaunchedListener(() => {36 Navigation.setRoot({37 root: {38 bottomTabs: {39 {40 stack: {41 {42 component: {43 options: {44 topBar: {45 title: {46 },47 },48 },49 },50 },51 },52 },53 {54 component: {55 options: {56 topBar: {57 title: {58 },59 },60 },61 },62 },63 },64 },65 });66});67Navigation.events().registerAppLaunchedListener(() => {68 Navigation.setRoot({69 root: {70 stack: {71 {72 component: {73 options: {74 topBar: {75 title: {76 },77 },78 },79 },80 },81 },82 },83 });84});85Navigation.events().registerAppLaunchedListener(() => {86 Navigation.setRoot({87 root: {88 stack: {89 {90 component: {91 options: {92 topBar: {93 title: {94 },95 },96 },97 },98 },99 },100 },101 });102});103Navigation.events().registerAppLaunchedListener(() => {
Using AI Code Generation
1import { Navigation } from 'react-native-navigation';2import { Navigation } from 'react-native-navigation';3import { registerScreens } from './screens';4registerScreens();5Navigation.events().registerAppLaunchedListener(() => {6 Navigation.setRoot({7 root: {8 stack: {9 {10 component: {11 }12 }13 options: {14 topBar: {15 title: {16 }17 }18 }19 }20 }21 });22});23Navigation.events().registerAppLaunchedListener(() => {24 Navigation.setRoot({25 root: {26 stack: {27 {28 component: {29 }30 }31 options: {32 topBar: {33 title: {34 }35 }36 }37 }38 }39 });40});41Navigation.events().registerAppLaunchedListener(() => {42 Navigation.setRoot({43 root: {44 stack: {45 {46 component: {47 }48 }49 options: {50 topBar: {51 title: {52 }53 }54 }55 }56 }57 });58});59Navigation.events().registerAppLaunchedListener(() => {60 Navigation.setRoot({61 root: {62 stack: {63 {64 component: {65 }66 }67 options: {68 topBar: {69 title: {70 }71 }72 }73 }74 }75 });76});77Navigation.events().registerAppLaunchedListener(() => {78 Navigation.setRoot({79 root: {80 stack: {81 {82 component: {83 }84 }85 options: {86 topBar: {87 title: {88 }89 }90 }91 }92 }93 });94});95Navigation.events().registerAppLaunchedListener(() => {96 Navigation.setRoot({97 root: {98 stack: {99 {100 component: {101 }102 }
Using AI Code Generation
1import {NavigationActions} from 'react-navigation';2const backAction = NavigationActions.back({});3this.props.navigation.dispatch(backAction);4const RootNavigator = StackNavigator({5 Login: {6 navigationOptions: {7 }8 },9 Home: {10 navigationOptions: {11 }12 },13 Profile: {14 navigationOptions: {15 }16 }17}, {18 onTransitionStart: () => {19 console.log('onTransitionStart');20 },21 onTransitionEnd: () => {22 console.log('onTransitionEnd');23 },24 onTerminateApp: () => {25 console.log('onTerminateApp');26 }27});28import {RootNavigator} from './navigation/rootNavigator';29export default class App extends Component {30 render() {31 return (32 );33 }34}35import {NavigationActions} from 'react-navigation';36export default class Login extends Component {37 constructor(props) {38 super(props);39 this.state = {40 };41 }42 onLoginPress = () => {43 const {username, password} = this.state;44 if(username === 'admin' && password === 'admin') {45 const resetAction = NavigationActions.reset({46 NavigationActions.navigate({ routeName: 'Home'})47 });48 this.props.navigation.dispatch(resetAction);49 } else {50 Alert.alert('Error', 'Invalid username or password');51 }52 }53 render() {54 return (55 <View style={styles.container}>56 <Text style={styles.welcome}>57 style={styles.textInput}58 onChangeText={(username) => this.setState({username})}59 value={this.state.username}60 style={styles.textInput}61 onChangeText={(password) => this.setState({password})}62 value={this.state.password}
Using AI Code Generation
1import { rootNavigator } from './navigation';2export const onTerminateApp = () => {3 rootNavigator.dispatch(4 NavigationActions.navigate({5 }),6 );7};8import { navigationService } from './navigation';9export const onTerminateApp = () => {10 navigationService.navigate('Home');11};
Using AI Code Generation
1import {NavigationActions} from 'react-navigation';2export const onTerminateApp = () => {3 const resetAction = NavigationActions.reset({4 actions: [NavigationActions.navigate({ routeName: 'Login' })],5 });6 return resetAction;7};8import {onTerminateApp} from './test';9const App = () => {10 return (11 <RootNavigator onNavigationStateChange={null} onTerminateApp={onTerminateApp} />12 );13};14import { StackNavigator } from 'react-navigation';15export const RootNavigator = StackNavigator({16 Login: { screen: Login },17 Main: { screen: Main },18}, {19 navigationOptions: {20 },21});22RootNavigator.navigationOptions = ({ navigation }) => {23 let { routeName } = navigation.state.routes[navigation.state.index];24 let navigationOptions = {};25 if (routeName === 'Login') {26 navigationOptions.tabBarVisible = false;27 }28 if (navigation.state.params && navigation.state.params.onTerminateApp) {29 navigationOptions.onTerminateApp = navigation.state.params.onTerminateApp;30 }31 return navigationOptions;32};33import {NavigationActions} from 'react-navigation';34export const onTerminateApp = () => {35 const resetAction = NavigationActions.reset({36 actions: [NavigationActions.navigate({ routeName: 'Login' })],37 });38 return resetAction;39};40import {onTerminateApp} from './test';41const App = () => {42 return (
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!!