Best JavaScript code snippet using wpt
reducer.test.ts
Source:reducer.test.ts  
1/**2 * Copyright (c) 2019 Paul Armstrong3 */4import * as Actions from '../actions';5import Build from '@build-tracker/build';6import ColorScale from '../../modules/ColorScale';7import Comparator from '@build-tracker/comparator';8import reducer from '../reducer';9import { FetchState, GraphType, State } from '../types';10const initialState: State = Object.freeze({11  activeArtifacts: {},12  activeComparator: null,13  artifactConfig: {},14  budgets: [],15  builds: [],16  colorScale: Object.keys(ColorScale)[0],17  comparator: new Comparator({ builds: [] }),18  comparedRevisions: [],19  disabledArtifactsVisible: true,20  fetchState: FetchState.NONE,21  graphType: GraphType.AREA,22  hoveredArtifacts: [],23  hideAttribution: false,24  name: 'Tacos!',25  snacks: [],26  defaultSizeKey: 'stat',27  sizeKey: '',28  url: 'https://build-tracker.local',29});30const buildA = new Build({ branch: 'main', revision: '123', parentRevision: '000', timestamp: Date.now() - 400 }, [31  { name: 'tacos', hash: '123', sizes: { stat: 123, gzip: 45 } },32]);33const buildB = new Build({ branch: 'main', revision: 'abc', parentRevision: '123', timestamp: Date.now() }, [34  { name: 'tacos', hash: 'abc', sizes: { stat: 125, gzip: 47 } },35]);36describe('reducer', () => {37  describe('active artifacts', () => {38    test('updates the active state of artifacts', () => {39      const state = reducer(40        { ...initialState, activeArtifacts: { churros: false } },41        Actions.setArtifactActive(['tacos', 'burritos'], true)42      );43      expect(state.activeArtifacts).toEqual({ churros: false, tacos: true, burritos: true });44    });45  });46  describe('set builds', () => {47    test('stores builds', () => {48      const state = reducer(initialState, Actions.setBuilds([buildA, buildB]));49      expect(state.builds).toEqual([buildA, buildB]);50    });51    test('sets a new comparator with the given builds', () => {52      const state = reducer(initialState, Actions.setBuilds([buildA, buildB]));53      expect(state.comparator).toBeInstanceOf(Comparator);54      expect(state.comparator.artifactNames).toEqual(['tacos']);55      expect(state.activeComparator).toBeInstanceOf(Comparator);56    });57    test('includes filters in the comparator', () => {58      const state = reducer(59        { ...initialState, artifactConfig: { filters: [/tacos/] } },60        Actions.setBuilds([buildA, buildB])61      );62      expect(state.comparator).toBeInstanceOf(Comparator);63      expect(state.comparator.artifactNames).toEqual([]);64      expect(state.activeComparator).toBeInstanceOf(Comparator);65      expect(state.activeComparator.artifactNames).toEqual([]);66    });67    test('activates all artifacts', () => {68      const state = reducer(initialState, Actions.setBuilds([buildA, buildB]));69      expect(state.activeArtifacts).toEqual({ tacos: true });70    });71    test('carries over active artifacts, disables inactive or unknown', () => {72      const buildA = new Build(73        { branch: 'main', revision: '123', parentRevision: '000', timestamp: Date.now() - 400 },74        [75          { name: 'tacos', hash: '123', sizes: { stat: 123, gzip: 45 } },76          { name: 'burritos', hash: '123', sizes: { stat: 123, gzip: 45 } },77        ]78      );79      const state = reducer({ ...initialState, activeArtifacts: { tacos: true } }, Actions.setBuilds([buildA, buildB]));80      expect(state.activeArtifacts).toEqual({ tacos: true, burritos: false });81    });82    test('creates an activeComparator if all current compared revisions are in the new builds', () => {83      const state = reducer({ ...initialState, comparedRevisions: ['123'] }, Actions.setBuilds([buildA, buildB]));84      expect(state.activeComparator).toBeInstanceOf(Comparator);85    });86    test('nulls out activeComparator if current compared revisions are not in the new builds', () => {87      const state = reducer({ ...initialState, comparedRevisions: ['foobar'] }, Actions.setBuilds([buildA, buildB]));88      expect(state.activeComparator).toBeNull();89    });90    test('keeps the current sizeKey if it is in the new set', () => {91      const state = reducer({ ...initialState, sizeKey: 'stat' }, Actions.setBuilds([buildA, buildB]));92      expect(state.sizeKey).toBe('stat');93    });94    test('resets the sizeKey to the default if the current is not in the new set', () => {95      const state = reducer({ ...initialState, sizeKey: 'foobar' }, Actions.setBuilds([buildA, buildB]));96      expect(state.sizeKey).toBe('stat');97    });98    test('resets the sizeKey to the first in list if both the current and the default are not in the new set', () => {99      const state = reducer(100        { ...initialState, defaultSizeKey: 'abc', sizeKey: 'foobar' },101        Actions.setBuilds([buildA, buildB])102      );103      expect(state.sizeKey).toBe('gzip');104    });105    test('sets graph type to bar if 10 or fewer builds', () => {106      const state = reducer({ ...initialState, sizeKey: 'foobar' }, Actions.setBuilds([buildA, buildB]));107      expect(state.graphType).toBe(GraphType.STACKED_BAR);108    });109  });110  describe('set color scale', () => {111    test('sets the color scale', () => {112      const state = reducer(initialState, Actions.setColorScale('Cool'));113      expect(state.colorScale).toEqual('Cool');114    });115  });116  describe('compared revisions', () => {117    test('adding updates the active comparator', () => {118      const state = reducer({ ...initialState, builds: [buildA, buildB] }, Actions.addComparedRevision('123'));119      expect(state.comparedRevisions).toEqual(['123']);120      expect(state.activeComparator.builds.map((b) => b.getMetaValue('revision'))).toEqual(['123']);121    });122    test('removing updates the active comparator', () => {123      const state = reducer(124        { ...initialState, activeComparator: new Comparator({ builds: [buildA] }), builds: [buildA, buildB] },125        Actions.removeComparedRevision('123')126      );127      expect(state.comparedRevisions).toEqual([]);128      expect(state.activeComparator.builds).toEqual([]);129    });130    test('removing unfocuses the build if it is focused', () => {131      const state = reducer(132        {133          ...initialState,134          activeComparator: new Comparator({ builds: [buildA] }),135          builds: [buildA, buildB],136          comparedRevisions: ['123'],137          focusedRevision: '123',138        },139        Actions.removeComparedRevision('123')140      );141      expect(state.focusedRevision).toBeUndefined();142    });143    test('clearing clears all', () => {144      const state = reducer(145        {146          ...initialState,147          activeComparator: new Comparator({ builds: [buildA, buildB] }),148          builds: [buildA, buildB],149          comparedRevisions: ['123', 'abc'],150          focusedRevision: '123',151        },152        Actions.clearComparedRevisions()153      );154      expect(state.focusedRevision).toBeUndefined();155      expect(state.comparedRevisions).toEqual([]);156      expect(state.activeComparator).toBeNull();157    });158  });159  describe('set focused revision', () => {160    test('sets the revision', () => {161      const state = reducer(initialState, Actions.setFocusedRevision('123'));162      expect(state.focusedRevision).toBe('123');163    });164    test('can clear', () => {165      const state = reducer(initialState, Actions.setFocusedRevision());166      expect(state.focusedRevision).toBeUndefined();167    });168  });169  describe('disabled artifacts visibility', () => {170    test('can be toggled', () => {171      const state = reducer(172        { ...initialState, disabledArtifactsVisible: true },173        Actions.setDisabledArtifactsVisible(false)174      );175      expect(state.disabledArtifactsVisible).toBe(false);176    });177  });178  describe('set size key', () => {179    test('sets the size key', () => {180      const state = reducer({ ...initialState, sizeKey: 'tacos' }, Actions.setSizeKey('burritos'));181      expect(state.sizeKey).toBe('burritos');182    });183  });184  describe('snacks', () => {185    test('can be added', () => {186      const state = reducer({ ...initialState, snacks: ['tacos'] }, Actions.addSnack('burritos'));187      expect(state.snacks).toEqual(['tacos', 'burritos']);188    });189    test('can be removed', () => {190      const state = reducer(191        { ...initialState, snacks: ['tacos', 'churros', 'burritos'] },192        Actions.removeSnack('churros')193      );194      expect(state.snacks).toEqual(['tacos', 'burritos']);195    });196  });197  describe('hovered artifacts', () => {198    test('can be set', () => {199      const state = reducer(initialState, Actions.setHoveredArtifacts(['burritos', 'tacos']));200      expect(state.hoveredArtifacts).toEqual(['burritos', 'tacos']);201    });202    test('does not modify state if new hovered are equal', () => {203      const mockState = { ...initialState, hoveredArtifacts: ['tacos', 'burritos'] };204      const state = reducer(mockState, Actions.setHoveredArtifacts(['burritos', 'tacos']));205      expect(state).toBe(mockState);206    });207  });208  describe('set graph type', () => {209    test('can be set', () => {210      const state = reducer(initialState, Actions.setGraphType(GraphType.STACKED_BAR));211      expect(state.graphType).toEqual(GraphType.STACKED_BAR);212    });213  });214  describe('unknown actions', () => {215    test('returns the state', () => {216      // @ts-ignore217      expect(reducer(initialState, {})).toEqual(initialState);218    });219  });...BuildDelta.test.ts
Source:BuildDelta.test.ts  
1/**2 * Copyright (c) 2019 Paul Armstrong3 */4import ArtifactDelta from '../ArtifactDelta';5import Build from '@build-tracker/build';6import BuildDelta from '../BuildDelta';7import { BudgetLevel, BudgetType } from '@build-tracker/types';8describe('BuildDelta', () => {9  let buildA, buildB;10  beforeEach(() => {11    jest.spyOn(Date, 'now').mockReturnValue(1550528708828);12    buildA = new Build(13      {14        branch: 'main',15        revision: { value: '123', url: 'https://build-tracker.local' },16        parentRevision: 'abc',17        timestamp: Date.now(),18      },19      [20        { name: 'tacos', hash: '123', sizes: { stat: 2, gzip: 1 } },21        { name: 'burritos', hash: '123', sizes: { stat: 3, gzip: 2 } },22      ]23    );24    buildB = new Build(25      {26        branch: 'main',27        revision: { value: '456', url: 'https://build-tracker.local' },28        parentRevision: 'abc',29        timestamp: Date.now(),30      },31      [32        { name: 'tacos', hash: '123', sizes: { stat: 1, gzip: 1 } },33        { name: 'burritos', hash: 'abc', sizes: { stat: 6, gzip: 4 } },34        { name: 'churros', hash: 'abc', sizes: { stat: 6, gzip: 4 } },35      ]36    );37  });38  describe('baseBuild', () => {39    test('gets the base build', () => {40      const bd = new BuildDelta(buildA, buildB);41      expect(bd.baseBuild).toBe(buildA);42    });43  });44  describe('prevBuild', () => {45    test('gets the prev build', () => {46      const bd = new BuildDelta(buildA, buildB);47      expect(bd.prevBuild).toBe(buildB);48    });49  });50  describe('artifactSizes', () => {51    test('gets a list of size keys available', () => {52      const bd = new BuildDelta(buildA, buildB);53      expect(bd.artifactSizes).toEqual(['stat', 'gzip']);54    });55  });56  describe('artifactNames', () => {57    test('gets a set of artifact names', () => {58      const bd = new BuildDelta(buildA, buildB);59      expect(Array.from(bd.artifactNames)).toEqual(['tacos', 'burritos', 'churros']);60    });61    test('filters out filtered artifact names', () => {62      const bd = new BuildDelta(buildA, buildB, { artifactFilters: [/churros/] });63      expect(Array.from(bd.artifactNames)).toEqual(['tacos', 'burritos']);64    });65  });66  describe('artifactDeltas', () => {67    test('gets an array of deltas for all artifacts', () => {68      const bd = new BuildDelta(buildA, buildB);69      expect(bd.artifactDeltas).toEqual([70        new ArtifactDelta('tacos', [], { gzip: 1, stat: 2 }, { gzip: 1, stat: 1 }, false),71        new ArtifactDelta('burritos', [], { gzip: 2, stat: 3 }, { gzip: 4, stat: 6 }, true),72        new ArtifactDelta('churros', [], { gzip: 0, stat: 0 }, { gzip: 4, stat: 6 }, true),73      ]);74    });75    test('artifact deltas when adding artifacts', () => {76      const bd = new BuildDelta(buildB, buildA);77      expect(bd.artifactDeltas).toEqual([78        new ArtifactDelta('tacos', [], { gzip: 1, stat: 1 }, { gzip: 1, stat: 2 }, false),79        new ArtifactDelta('burritos', [], { gzip: 4, stat: 6 }, { gzip: 2, stat: 3 }, true),80        new ArtifactDelta('churros', [], { gzip: 4, stat: 6 }, { gzip: 0, stat: 0 }, true),81      ]);82    });83    test('respects artifact filters', () => {84      const bd = new BuildDelta(buildA, buildB, { artifactFilters: [/churros/, /burritos/] });85      expect(bd.artifactDeltas).toEqual([86        new ArtifactDelta('tacos', [], { gzip: 1, stat: 2 }, { gzip: 1, stat: 1 }, false),87      ]);88    });89    test('includes budgets for each artifact', () => {90      const budgets = [{ level: BudgetLevel.WARN, sizeKey: 'gzip', type: BudgetType.DELTA, maximum: 1 }];91      const bd = new BuildDelta(buildA, buildB, {92        artifactBudgets: { tacos: budgets },93      });94      expect(bd.artifactDeltas).toEqual(95        expect.arrayContaining([new ArtifactDelta('tacos', budgets, { gzip: 1, stat: 2 }, { gzip: 1, stat: 1 }, false)])96      );97    });98    test('includes * budgets on every artifact', () => {99      const budget = { level: BudgetLevel.WARN, sizeKey: 'gzip', type: BudgetType.DELTA, maximum: 1 };100      const burritoBudget = { level: BudgetLevel.ERROR, sizeKey: 'stat', type: BudgetType.DELTA, maximum: 1 };101      const bd = new BuildDelta(buildA, buildB, { artifactBudgets: { '*': [budget], burritos: [burritoBudget] } });102      expect(bd.artifactDeltas).toEqual([103        new ArtifactDelta('tacos', [budget], { gzip: 1, stat: 2 }, { gzip: 1, stat: 1 }, false),104        new ArtifactDelta(105          'burritos',106          expect.arrayContaining([budget, burritoBudget]),107          { gzip: 2, stat: 3 },108          { gzip: 4, stat: 6 },109          true110        ),111        new ArtifactDelta('churros', [budget], { gzip: 0, stat: 0 }, { gzip: 4, stat: 6 }, true),112      ]);113    });114  });115  describe('getArtifactDelta', () => {116    test('gets the delta for a single artifact', () => {117      const bd = new BuildDelta(buildA, buildB);118      expect(bd.getArtifactDelta('tacos')).toEqual(119        new ArtifactDelta('tacos', [], { gzip: 1, stat: 2 }, { gzip: 1, stat: 1 }, false)120      );121    });122  });123  describe('getGroupDelta', () => {124    test('gets the delta for a defined group', () => {125      const bd = new BuildDelta(buildA, buildB, { groups: [{ name: 'stuff', artifactNames: ['burritos', 'tacos'] }] });126      expect(bd.getGroupDelta('stuff')).toEqual(127        new ArtifactDelta('stuff', [], { stat: 5, gzip: 3 }, { stat: 7, gzip: 5 }, true)128      );129    });130    test('calculates sum and hash changes with regex artifact match', () => {131      const bd = new BuildDelta(buildA, buildB, {132        groups: [{ name: 'stuff', artifactNames: ['burritos'], artifactMatch: /^tac/ }],133      });134      expect(bd.getGroupDelta('stuff')).toEqual(135        new ArtifactDelta('stuff', [], { stat: 5, gzip: 3 }, { stat: 7, gzip: 5 }, true)136      );137    });138  });...generate-gulp-tasks.test.js
Source:generate-gulp-tasks.test.js  
1const pick = require('lodash/pick');2const { generateTaskList } = require('../lib/generate-gulp-tasks');3const { BEFORE_BUILD, BUILD, AFTER_BUILD, BEFORE_WATCH, WATCH, AFTER_WATCH } = require('../lib/task-order');4test('Tasks are organized correctly', () => {5    const beforeBuildA = () => {};6    const beforeBuildB = () => {};7    const buildA = () => {};8    const buildB = () => {};9    const beforeWatchA = () => {};10    const watchA = () => {};11    const afterBuildA = () => {};12    const taskConfig = {'tasks': {13        'task1': [14            {[BEFORE_BUILD]: beforeBuildA},15            {[BEFORE_BUILD]: beforeBuildB},16        ],17        'task2': [18            {[BUILD]: buildA, [WATCH]: watchA}19        ],20        'task3': [21            {[BUILD]: buildB, [AFTER_BUILD]: afterBuildA}22        ],23        'task4': [24            {[BEFORE_WATCH]: beforeWatchA}25        ],26    }};27    expect(pick(generateTaskList(taskConfig), ['default', 'build'])).toEqual({28        default: [29            [beforeBuildA, beforeBuildB],30            [buildA, buildB],31            [beforeWatchA],32            [watchA],33        ],34        build: [35            [beforeBuildA, beforeBuildB],36            [buildA, buildB],37            [afterBuildA],38        ]39    });40});41test('Skipping tasks doesn\'t genearate empty arrays', () => {42    const beforeBuildA = () => {};43    const beforeBuildB = () => {};44    const buildA = () => {};45    const buildB = () => {};46    const afterBuildA = () => {};47    const taskConfig = {'tasks': {48        'task1': [49            {[BEFORE_BUILD]: beforeBuildA},50            {[BEFORE_BUILD]: beforeBuildB},51        ],52        'task2': [53            {[BUILD]: buildA}54        ],55        'task3': [56            {[BUILD]: buildB, [AFTER_BUILD]: afterBuildA}57        ],58    }};59    const taskList = pick(generateTaskList(taskConfig), ['default', 'build']);60    for (let taskName in taskList) {61        taskList[taskName].forEach((parallel) => {62            expect(parallel.length).not.toBe(0);63        });64    }...Using AI Code Generation
1var wpt = require('wpt');2var wpt = new WebPageTest('www.webpagetest.org', 'A.1a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p7q8r9s0t1u2v3w4x5y6z7a8b9c0d1e2f3g4h5i6j7k8l9m0n1o2p3q4r5s6t7u8v9w0x1y2z3');3  if(err) {4    console.log(err);5  } else {6    console.log(data);7  }8});9var wpt = require('wpt');10var wpt = new WebPageTest('www.webpagetest.org', 'A.1a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p7q8r9s0t1u2v3w4x5y6z7a8b9c0d1e2f3g4h5i6j7k8l9m0n1o2p3q4r5s6t7u8v9w0x1y2z3');11  if(err) {12    console.log(err);13  } else {14    console.log(data);15  }16});17var wpt = require('wpt');18var wpt = new WebPageTest('www.webpagetest.org', 'A.1a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p7q8r9s0t1u2v3w4x5y6z7a8b9c0d1e2f3g4h5i6j7k8l9m0n1o2p3qUsing AI Code Generation
1var wpt = require('webpagetest');2var options = { host: 'www.webpagetest.org', key: 'A.12345678901234567890123456789012' };3var wpt = new WebPageTest(options);4wpt.buildB('www.webpagetest.org/', function(err, data) {5});6### WebPageTest(options)7* `host` - the server to connect to (default: `www.webpagetest.org`)8* `key` - your API key (default: `null`)9### WebPageTest#runTest(url, options, callback)10* `location` - the location to test from (default: `Dulles:Chrome`)11* `firstViewOnly` - set to true to only test the first view (default: `false`)12* `runs` - the number of test runs (default: `3`)13* `connectivity` - the connectivity profile (default: `Cable`)14* `pollResults` - set to true to poll for test results (default: `false`)15* `pollInterval` - the interval in milliseconds to poll for test results (default: `5000`)16* `timeout` - the maximum time to wait for test results in milliseconds (default: `300000`)17* `video` - set to true to capture video (default: `false`)18  * `screenWidth` - the width of the video (default: `1024`)19  * `screenHeight` - the height of the video (default: `768`)20  * `videoQuality` - the quality of the video (default: `2`)21  * `videoCodec` - the codec to use for the video (default: `VP8`)22  * `fullResolution` - set to true to use full resolution (default: `false`)23  * `mobile` - set to true to emulate a mobile device (default: `false`)24  * `timeline` - set to true to capture a timeline (default: `false`)25  * `trace` - set to true to capture a trace (default: `false`)Using AI Code Generation
1var wpt = require('wpt');2var wpt = new WebPageTest('www.webpagetest.org');3wpt.buildB('www.webpagetest.org', function(err, data) {4  console.log(data);5});6var wpt = require('wpt');7var wpt = new WebPageTest('www.webpagetest.org');8wpt.buildB('www.webpagetest.orgUsing AI Code Generation
1var wptools = require('wptools');2var options = {3};4wptools.page('Barack_Obama').get(options, function(err, resp) {5  console.log(resp);6});Using AI Code Generation
1var wpt = require("wpt");2var wpt = new WebPageTest('www.webpagetest.org');3  if (err) return console.error(err);4  console.log(data);5});6var wpt = require("wpt");7var wpt = new WebPageTest('www.webpagetest.org');8  if (err) return console.error(err);9  console.log(data);10});11var wpt = require("wpt");12var wpt = new WebPageTest('www.webpagetest.org');13  if (err) return console.error(err);14  console.log(data);15});16var wpt = require("wpt");17var wpt = new WebPageTest('www.webpagetest.org');18  if (err) return console.error(err);19  console.log(data);20});21var wpt = require("wpt");22var wpt = new WebPageTest('www.webpagetest.org');23  if (err) return console.error(err);24  console.log(data);25});26var wpt = require("wpt");27var wpt = new WebPageTest('www.webpagetest.org');28  if (err) return console.error(err);29  console.log(data);30});31var wpt = require("wpt");32var wpt = new WebPageTest('www.webpagetest.org');33  if (err) return console.error(err);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!!
