Best JavaScript code snippet using wpt
unit-test-script.js
Source:unit-test-script.js
1window.addEventListener("load", init_tests);2function init_tests () {3 console.log("init_tests");4 run_test( test_1 );5 run_test( test__add );6 // run_test( test__show_result );7 // run_test( test__get_random_number__str );8 // run_test( test__get_random_number__1_6 );9}10function run_test ( test_name ) {11 let test_result = test_name("foo");12 console.log(test_name.name, "test result: ", test_result);13}14function test_1 ( val ) {15 console.log("test_1");16 return val;17}18// testing `result` function19// expect input value to be displayed as value of DOM element20function test__show_result () {21 // test setup22 const val = "hello";23 show_result(val);24 const result_el = document.getElementById("result");25 const result_val = result_el.textContent;26 let test_result = "fail";27 // test condition28 if (result_val === val) {29 test_result = "pass";30 }31 32 return test_result;33}34// testing `get_random_number` function35// expect string argument to return 0 36function test__get_random_number__str () {37 // test setup38 const test_param = "hello";39 const result_val = get_random_number(test_param);40 let test_result = "fail";41 // test condition42 const expected_val = 0;43 if (result_val === expected_val) {44 test_result = "pass";45 }46 // test expectations47 console.log(`${arguments.callee.name} status: ${test_result}`, 48 {49 "_function": get_random_number.name,50 test_param,51 expected_val,52 result_val53 }54 );55 56 return test_result;57}58// testing `get_random_number` function59// expect argments (1,6) argument to return number greater than or equal to 1, and less than or equal to 6 60function test__get_random_number__1_6 () {61 // test setup62 const test_param_1 = 1;63 const test_param_2 = 6;64 const result_val = get_random_number(test_param_1, test_param_2);65 let test_result = "fail";66 // test condition67 const expected_val_1 = 1;68 const expected_val_2 = 6;69 if (result_val >= expected_val_170 && result_val <= expected_val_2) {71 test_result = "pass";72 }73 // test expectations74 console.log(`${arguments.callee.name} status: ${test_result}`, 75 {76 "_function": get_random_number.name,77 test_param_1,78 test_param_2,79 expected_val_1,80 expected_val_2,81 result_val82 }83 );84 85 return test_result;86}87// testing `add` function88// expect (1,2,3) argument to return 6 89function test__add () {90 // test setup91 const params = [1, 2, 3];92 const result_val = add(...params);93 let test_result = "fail";94 // test condition95 const expected_val = 6;96 if (result_val === expected_val) {97 test_result = "pass";98 }99 100 return test_result;...
initialState.ts
Source:initialState.ts
1type TCallback = (passes: boolean) => void;2export type TState = {3 // Indicate the tests sttarted4 isRunning: boolean,5 // Number of not started tests6 notStarted: number,7 // Number of passed tests8 passed: number,9 // Number of failes tests10 failed: number,11 tests: TTest[],12}13export type TTest = {14 id: number;15 description: string;16 run: (callback: TCallback) => void;17 passed: null | boolean;18 running: boolean;19};20const makeDummyTest = () => {21 const delay = 7000 + Math.random() * 7000;22 const testPassed = Math.random() > 0.5;23 return (callback: TCallback) => {24 window.setTimeout(() => callback(testPassed), delay);25 };26};27const INIT_TESTS: TTest[] = [28 { id: 1, description: "uploads go in both directions", run: makeDummyTest(), passed: null, running: false },29 { id: 2, description: "PDFs are adequately waterproof", run: makeDummyTest(), passed: null, running: false },30 { id: 3, description: "videos are heated to 12,000,000 Kelvin", run: makeDummyTest(), passed: null, running: false },31 { id: 4, description: "subpixels can go rock climbing", run: makeDummyTest(), passed: null, running: false },32 { id: 5, description: "images are squarer than traffic cones", run: makeDummyTest(), passed: null, running: false },33 { id: 6, description: "metaproperties don't go too meta", run: makeDummyTest(), passed: null, running: false },34];35export const initialState: TState = {36 isRunning: false,37 notStarted: INIT_TESTS.length,38 passed: 0,39 failed: 0,40 tests: INIT_TESTS,...
App.js
Source:App.js
1import React from "react";2import { useState } from "react";3import { Navbar, Nav, Container } from "react-bootstrap";4import Actions from "./Actions";5import Tests from "./Tests";6import "bootstrap/dist/css/bootstrap.min.css";7import "./App.css";8export const BASE_URL = "http://localhost:5000/api";9function App() {10 let cache = localStorage.getItem("cache");11 let init_tests = cache ? JSON.parse(cache).tests : [];12 const [tests, setTests] = useState(init_tests);13 const [lock, setLock] = useState(true);14 localStorage.setItem(15 "cache",16 JSON.stringify({17 tests,18 })19 );20 return (21 <div className="App">22 <Navbar>23 <Navbar.Brand href="#">24 <img25 src="/assets/logo.png"26 width="92"27 height="92"28 className="d-inline-block align-top"29 alt="pytest logo"30 />31 </Navbar.Brand>32 {false && <Nav>33 <Nav.Item>34 <Nav.Link href="#reports">reports</Nav.Link>35 </Nav.Item>36 </Nav>}37 </Navbar>38 <Container className="inner">39 <Actions tests={tests} setTests={setTests} lock={lock} />40 <Tests41 tests={tests}42 setTests={setTests}43 lock={lock}44 setLock={setLock}45 />46 </Container>47 </div>48 );49}...
Using AI Code Generation
1init_tests({explicit_done: true});2async_test(function(t) {3 var iframe = document.createElement('iframe');4 iframe.src = 'data:text/html,';5 iframe.onload = t.step_func_done(function() {6 iframe.contentWindow.postMessage('test', '*');7 });8 document.body.appendChild(iframe);9}, 'Test to verify postMessage works with data:text/html,');
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!!