Best JavaScript code snippet using backstopjs
TreeView-story.js
Source:TreeView-story.js
1/**2 * Copyright IBM Corp. 2016, 20183 *4 * This source code is licensed under the Apache-2.0 license found in the5 * LICENSE file in the root directory of this source tree.6 */7import React, { useState } from 'react';8import { Document16, Folder16 } from '@carbon/icons-react';9import { action } from '@storybook/addon-actions';10import {11 boolean,12 object,13 select,14 text,15 withKnobs,16} from '@storybook/addon-knobs';17import { InlineNotification } from '../Notification';18import TreeView, { TreeNode } from '../TreeView';19const sizes = {20 default: 'default',21 compact: 'compact',22};23const props = () => ({24 active: text('Active node ID (active)', '5'),25 hideLabel: boolean('Visible label (hideLabel)', false),26 label: text('Label (label)', 'Tree view'),27 multiselect: boolean(28 'Allow selection of multiple tree nodes (multiselect)',29 false30 ),31 onSelect: action('onSelect (TreeView onSelect)'),32 selected: object('Array of selected node IDs (selected)', ['5']),33 size: select('Tree size (sizes)', sizes, 'default'),34});35const nodes = [36 {37 id: '1',38 value: 'Artificial intelligence',39 label: <span>Artificial intelligence</span>,40 renderIcon: Document16,41 onSelect: action('onSelect (TreeNode onSelect)'),42 onToggle: action('onToggle'),43 },44 {45 id: '2',46 value: 'Blockchain',47 label: 'Blockchain',48 renderIcon: Document16,49 onSelect: action('onSelect (TreeNode onSelect)'),50 onToggle: action('onToggle'),51 },52 {53 id: '3',54 value: 'Business automation',55 label: 'Business automation',56 renderIcon: Folder16,57 onSelect: action('onSelect (TreeNode onSelect)'),58 onToggle: action('onToggle'),59 children: [60 {61 id: '3-1',62 value: 'Business process automation',63 label: 'Business process automation',64 renderIcon: Document16,65 onSelect: action('onSelect (TreeNode onSelect)'),66 onToggle: action('onToggle'),67 },68 {69 id: '3-2',70 value: 'Business process mapping',71 label: 'Business process mapping',72 renderIcon: Document16,73 onSelect: action('onSelect (TreeNode onSelect)'),74 onToggle: action('onToggle'),75 },76 ],77 },78 {79 id: '4',80 value: 'Business operations',81 label: 'Business operations',82 renderIcon: Document16,83 onSelect: action('onSelect (TreeNode onSelect)'),84 onToggle: action('onToggle'),85 },86 {87 id: '5',88 value: 'Cloud computing',89 label: 'Cloud computing',90 isExpanded: true,91 renderIcon: Folder16,92 onSelect: action('onSelect (TreeNode onSelect)'),93 onToggle: action('onToggle'),94 children: [95 {96 id: '5-1',97 value: 'Containers',98 label: 'Containers',99 renderIcon: Document16,100 onSelect: action('onSelect (TreeNode onSelect)'),101 onToggle: action('onToggle'),102 },103 {104 id: '5-2',105 value: 'Databases',106 label: 'Databases',107 renderIcon: Document16,108 onSelect: action('onSelect (TreeNode onSelect)'),109 onToggle: action('onToggle'),110 },111 {112 id: '5-3',113 value: 'DevOps',114 label: 'DevOps',115 isExpanded: true,116 renderIcon: Folder16,117 onSelect: action('onSelect (TreeNode onSelect)'),118 onToggle: action('onToggle'),119 children: [120 {121 id: '5-4',122 value: 'Solutions',123 label: 'Solutions',124 renderIcon: Document16,125 onSelect: action('onSelect (TreeNode onSelect)'),126 onToggle: action('onToggle'),127 },128 {129 id: '5-5',130 value: 'Case studies',131 label: 'Case studies',132 isExpanded: true,133 renderIcon: Folder16,134 onSelect: action('onSelect (TreeNode onSelect)'),135 onToggle: action('onToggle'),136 children: [137 {138 id: '5-6',139 value: 'Resources',140 label: 'Resources',141 renderIcon: Document16,142 onSelect: action('onSelect (TreeNode onSelect)'),143 onToggle: action('onToggle'),144 },145 ],146 },147 ],148 },149 ],150 },151 {152 id: '6',153 value: 'Data & Analytics',154 label: 'Data & Analytics',155 renderIcon: Folder16,156 onSelect: action('onSelect (TreeNode onSelect)'),157 onToggle: action('onToggle'),158 children: [159 {160 id: '6-1',161 value: 'Big data',162 label: 'Big data',163 renderIcon: Document16,164 onSelect: action('onSelect (TreeNode onSelect)'),165 onToggle: action('onToggle'),166 },167 {168 id: '6-2',169 value: 'Business intelligence',170 label: 'Business intelligence',171 renderIcon: Document16,172 onSelect: action('onSelect (TreeNode onSelect)'),173 onToggle: action('onToggle'),174 },175 ],176 },177 {178 id: '7',179 value: 'IT infrastructure',180 label: 'IT infrastructure',181 isExpanded: true,182 disabled: true,183 renderIcon: Folder16,184 onSelect: action('onSelect (TreeNode onSelect)'),185 onToggle: action('onToggle'),186 children: [187 {188 id: '7-1',189 value: 'Data storage',190 label: 'Data storage',191 renderIcon: Document16,192 onSelect: action('onSelect (TreeNode onSelect)'),193 onToggle: action('onToggle'),194 },195 {196 id: '7-2',197 value: 'Enterprise servers',198 label: 'Enterprise servers',199 renderIcon: Document16,200 onSelect: action('onSelect (TreeNode onSelect)'),201 onToggle: action('onToggle'),202 },203 {204 id: '8',205 value: 'Hybrid cloud infrastructure',206 label: 'Hybrid cloud infrastructure',207 isExpanded: true,208 renderIcon: Folder16,209 onSelect: action('onSelect (TreeNode onSelect)'),210 onToggle: action('onToggle'),211 children: [212 {213 id: '8-1',214 value: 'Insights',215 label: 'Insights',216 renderIcon: Document16,217 onSelect: action('onSelect (TreeNode onSelect)'),218 onToggle: action('onToggle'),219 },220 {221 id: '8-2',222 value: 'Benefits',223 label: 'Benefits',224 renderIcon: Document16,225 onSelect: action('onSelect (TreeNode onSelect)'),226 onToggle: action('onToggle'),227 },228 ],229 },230 ],231 },232];233function renderTree({ nodes, expanded, withIcons = false }) {234 if (!nodes) {235 return;236 }237 return nodes.map(({ children, renderIcon, isExpanded, ...nodeProps }) => (238 <TreeNode239 key={nodeProps.id}240 renderIcon={withIcons ? renderIcon : null}241 isExpanded={expanded ?? isExpanded}242 {...nodeProps}>243 {renderTree({ nodes: children, expanded, withIcons })}244 </TreeNode>245 ));246}247export default {248 title: 'unstable_TreeView',249 decorators: [withKnobs],250 parameters: { component: TreeView },251};252export const Default = () => (253 <>254 <InlineNotification255 kind="info"256 title="Experimental component"257 subtitle="An accessibility review of this component is in progress"258 />259 <TreeView {...props()}>{renderTree({ nodes })}</TreeView>260 </>261);262Default.storyName = 'default';263Default.parameters = {264 info: {265 text: ``,266 },267};268export const WithIcons = () => (269 <>270 <InlineNotification271 kind="info"272 title="Experimental component"273 subtitle="An accessibility review of this component is in progress"274 />275 <TreeView {...props()}>{renderTree({ nodes, withIcons: true })}</TreeView>276 </>277);278WithIcons.storyName = 'with icons';279export const WithControlledExpansion = () => {280 const [expanded, setExpanded] = useState(undefined);281 return (282 <>283 <InlineNotification284 kind="info"285 title="Experimental component"286 subtitle="An accessibility review of this component is in progress"287 />288 <div style={{ marginBottom: '1rem' }}>289 <button type="button" onClick={() => setExpanded(true)}>290 expand all291 </button>292 <button type="button" onClick={() => setExpanded(false)}>293 collapse all294 </button>295 </div>296 <TreeView {...props()}>{renderTree({ nodes, expanded })}</TreeView>297 </>298 );...
render-props.js
Source:render-props.js
...6 toggle = () =>7 this.setState(8 ({on}) => ({on: !on}),9 () => {10 this.props.onToggle(this.state.on)11 },12 )13 render() {14 const {on} = this.state15 const {onContent, offContent} = this.props16 return (17 <Fragment>18 {on ? onContent : offContent}19 <Switch on={on} onClick={this.toggle} />20 </Fragment>21 )22 }23}24/**/25//*26function Usage({onToggle = (...args) => console.log('onToggle', ...args)}) {27 return (28 <Toggle29 onToggle={onToggle}30 onContent="The button is on"31 offContent="The button is off"32 />33 )34}35/**/36/*37// what happens when you want to change where the content appears?38function Usage({onToggle = (...args) => console.log('onToggle', ...args)}) {39 return (40 <Toggle41 onToggle={onToggle}42 onContent="The button is on"43 offContent="The button is off"44 contentPosition="bottom"45 />46 )47}48/**/49/*50// what if you want the on and off content position in different places?51function Usage({onToggle = (...args) => console.log('onToggle', ...args)}) {52 return (53 <Toggle54 onToggle={onToggle}55 onContent="The button is on"56 offContent="The button is off"57 onContentPosition="top"58 offContentPosition="bottom"59 />60 )61}62/**/63// Oh, and what about a custom button!?64/*65// Here's the solution66class Toggle extends React.Component {67 state = {on: false}68 toggle = () =>69 this.setState(70 ({on}) => ({on: !on}),71 () => this.props.onToggle(this.state.on),72 )73 render() {74 return this.props.children({75 on: this.state.on,76 toggle: this.toggle,77 })78 }79}80function Usage({onToggle = (...args) => console.log('onToggle', ...args)}) {81 return (82 <Toggle onToggle={onToggle}>83 {({on, toggle}) => (84 <Fragment>85 {on ? 'The button is on' : 'The button is off'}...
Accordion.js
Source:Accordion.js
...24 const { onToggle } = this.props;25 if (!onToggle) {26 this.setState({ isOpen: !this.state.isOpen });27 } else {28 this.onToggle(!this.props.open);29 }30 }31 isOpen() {32 return !this.props.onToggle ?33 this.state.isOpen : this.props.open;34 }35 render() {36 /* eslint-disable-next-line no-unused-vars */37 const { className, children, initialOpen, ...otherProps } = this.props;38 return (39 <Provider40 value={{41 onToggle: this.toggleHandler.bind(this),42 isOpen: this.isOpen()...
Using AI Code Generation
1module.exports = async (page, scenario, vp) => {2 console.log('SCENARIO > ' + scenario.label);3 await require('./clickAndHoverHelper')(page, scenario);4 await page.waitFor(1000);5 await page.evaluate(() => {6 const el = document.querySelector('#toggle');7 if (el) {8 el.click();9 }10 });11 await page.waitFor(1000);12};13module.exports = async (page, scenario, vp) => {14 console.log('SCENARIO > ' + scenario.label);15 await require('./clickAndHoverHelper')(page, scenario);16 await page.waitFor(1000);17 await page.evaluate(() => {18 const el = document.querySelector('#toggle');19 if (el) {20 el.click();21 }22 });23 await page.waitFor(1000);24};25module.exports = async (page, scenario, vp) => {26 console.log('SCENARIO > ' + scenario.label);27 await require('./clickAndHoverHelper')(page, scenario);28 await page.waitFor(1000);29 await page.evaluate(() => {30 const el = document.querySelector('#toggle');31 if (el) {32 el.click();33 }34 });35 await page.waitFor(1000);36};37module.exports = async (page, scenario, vp) => {38 console.log('SCENARIO > ' + scenario.label);39 await require('./clickAndHoverHelper')(page, scenario);40 await page.waitFor(1000);41 await page.evaluate(() => {42 const el = document.querySelector('#toggle');43 if (el) {44 el.click();45 }46 });47 await page.waitFor(1000);48};49module.exports = async (page, scenario, vp) => {50 console.log('SCENARIO > ' + scenario.label);51 await require('./clickAndHoverHelper')(page, scenario);52 await page.waitFor(1000);53 await page.evaluate(() => {54 const el = document.querySelector('#toggle');55 if (el) {56 el.click();
Using AI Code Generation
1module.exports = async (page, scenario, vp) => {2 await require('./clickAndHoverHelper')(page, scenario);3 await page.evaluate(() => {4 document.querySelector('.toggle').click();5 });6};7module.exports = async (page, scenario) => {8 const hoverSelector = scenario.hoverSelector;9 const clickSelector = scenario.clickSelector;10 if (hoverSelector) {11 await page.waitForSelector(hoverSelector);12 await page.hover(hoverSelector);13 }14 if (clickSelector) {15 await page.waitForSelector(clickSelector);16 await page.click(clickSelector);17 }18 if (postInteractionWait) {19 await page.waitFor(postInteractionWait);20 }21};22{23 {24 },25 {26 },27 {28 },29 {30 },31 {32 }33 {34 }
Using AI Code Generation
1module.exports = async (page, scenario, vp) => {2 await page.evaluate((selector) => {3 document.querySelector(selector).click();4 }, scenario.onReadyScript.selector);5};6module.exports = async (page, scenario, vp) => {7 await page.evaluate((selector) => {8 document.querySelector(selector).click();9 }, scenario.onReadyScript.selector);10};11module.exports = async (page, scenario, vp) => {12 await page.evaluate((selector) => {13 document.querySelector(selector).click();14 }, scenario.onReadyScript.selector);15};16module.exports = async (page, scenario, vp) => {17 await page.evaluate((selector) => {18 document.querySelector(selector).click();19 }, scenario.onReadyScript.selector);20};21module.exports = async (page, scenario, vp) => {22 await page.evaluate((selector) => {23 document.querySelector(selector).click();24 }, scenario.onReadyScript.selector);25};26module.exports = async (page, scenario, vp) => {27 await page.evaluate((selector) => {28 document.querySelector(selector).click();29 }, scenario.onReadyScript.selector);30};31module.exports = async (page, scenario, vp) => {32 await page.evaluate((selector) => {33 document.querySelector(selector).click();34 }, scenario.onReadyScript.selector);35};36module.exports = async (page, scenario, vp) => {37 await page.evaluate((selector) => {38 document.querySelector(selector).click();39 }, scenario.onReadyScript.selector);40};41module.exports = async (page, scenario, vp) => {42 await page.evaluate((selector) => {43 document.querySelector(selector).click();44 }, scenario.onReadyScript.selector);45};46module.exports = async (page, scenario, vp) => {47 await page.evaluate((selector) => {
Using AI Code Generation
1module.exports = async (page, scenario) => {2 await require('./onReady.js')(page, scenario);3 await page.waitForSelector('body');4 await page.click('body');5 await page.waitFor(1000);6 await page.evaluate(() => {7 document.querySelector('body').click();8 });9 await page.waitFor(1000);10 await page.evaluate(() => {11 document.querySelector('body').click();12 });13 await page.waitFor(1000);14 await page.evaluate(() => {15 document.querySelector('body').click();16 });17 await page.waitFor(1000);18 await page.evaluate(() => {19 document.querySelector('body').click();20 });21 await page.waitFor(1000);22 await page.evaluate(() => {23 document.querySelector('body').click();24 });25 await page.waitFor(1000);26 await page.evaluate(() => {27 document.querySelector('body').click();28 });29 await page.waitFor(1000);30 await page.evaluate(() => {31 document.querySelector('body').click();32 });33 await page.waitFor(1000);34 await page.evaluate(() => {35 document.querySelector('body').click();36 });37 await page.waitFor(1000);38 await page.evaluate(() => {39 document.querySelector('body').click();40 });41 await page.waitFor(1000);42 await page.evaluate(() => {43 document.querySelector('body').click();44 });45 await page.waitFor(1000);46 await page.evaluate(() => {47 document.querySelector('body').click();48 });49 await page.waitFor(1000);50 await page.evaluate(() => {51 document.querySelector('body').click();52 });53 await page.waitFor(1000);54 await page.evaluate(() => {55 document.querySelector('body').click();56 });57 await page.waitFor(1000);58 await page.evaluate(() => {59 document.querySelector('body').click();60 });61 await page.waitFor(1000);62 await page.evaluate(() => {63 document.querySelector('body').click();64 });65 await page.waitFor(1000);66 await page.evaluate(() => {67 document.querySelector('body').click();68 });69 await page.waitFor(1000);70 await page.evaluate(() => {71 document.querySelector('body').click();72 });73 await page.waitFor(1000);74 await page.evaluate(() => {75 document.querySelector('body').click();76 });77};
Using AI Code Generation
1var fs = require('fs');2var path = require('path');3var config = require('./backstop.json');4var args = process.argv.slice(2);5var scenario = args[0];6var label = args[1];7var referenceUrl = config.scenarios[scenario].referenceUrl;8var testUrl = config.scenarios[scenario].testUrl;9var referenceFile = path.join(config.paths.bitmaps_reference, label, scenario + '_reference.png');10var testFile = path.join(config.paths.bitmaps_test, label, scenario + '_test.png');11var diffFile = path.join(config.paths.bitmaps_test, label, scenario + '_diff.png');12var htmlReportFile = path.join(config.paths.html_report, label, scenario + '_htmlReport.html');13var command = 'compare -metric AE ' + referenceFile + ' ' + testFile + ' ' + diffFile;14var exec = require('child_process').exec;15exec(command, function(error, stdout, stderr) {16 if (error) {17 console.error('exec error: ' + error);18 return;19 }20 var diff = parseInt(stdout);21 var diffPercentage = (diff / (config.viewports[scenario].width * config.viewports[scenario].height)) * 100;22 var data = fs.readFileSync(htmlReportFile, 'utf8');23 data = data.replace(/<td class="fail">[\s\S]*<\/td>/, '<td class="fail">' + diffPercentage + '%</td>');24 fs.writeFileSync(htmlReportFile, data, 'utf8');25});26"scenarios": [{27}]28"scenarios": [{
Using AI Code Generation
1I am trying to use onToggle method of backstopjs. I have imported the onToggle method in my test.js file. I have tried to use the onToggle method in my test.js file. But it is not working. Can anyone help me to fix this issue?2I am trying to use onToggle method of backstopjs. I have imported the onToggle method in my test.js file. I have tried to use the onToggle method in my test.js file. But it is not working. Can anyone help me to fix this issue?3You are trying to use the onToggle method of backstopjs. I have imported the onToggle method in my test.js file. I have tried to use the onToggle method in my test.js file. But it is not working. Can anyone help me to fix this issue?4I am trying to use the onToggle method of backstopjs. I have imported the onToggle method in my test.js file. I have tried to use the onToggle method in my test.js file. But it is not working. Can anyone help me to fix this issue?5I am trying to use the onToggle method of backstopjs. I have imported the onToggle method in my test.js file. I have tried to use the onToggle method in my test.js file. But it is not working. Can anyone help me to fix this issue?6I am trying to use the onToggle method of backstopjs. I have imported the onToggle method in my test.js file. I have tried to use the onToggle method in my test.js file. But it is not working. Can anyone help me to fix this issue?7I am trying to use the onToggle method of backstopjs. I have imported the onToggle method in my test.js file. I have tried to use the onToggle method in my test.js file. But it is not working. Can anyone help me to fix this
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!!