Best JavaScript code snippet using best
button.spec.js
Source:button.spec.js
1/*2 * Copyright (c) 2019, salesforce.com, inc.3 * All rights reserved.4 * SPDX-License-Identifier: MIT5 * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT6 */7import { createElement } from 'lwc';8import Element from 'lightning/button';9const createButton = (props = {}) => {10 const element = createElement('lightning-button', { is: Element });11 Object.assign(element, props);12 document.body.appendChild(element);13 return element;14};15describe('lightning-button', () => {16 it('should trigger focus when button is focused', () => {17 const element = createButton({});18 const evtListenerMock = jest.fn();19 element.addEventListener('focus', evtListenerMock);20 element.shadowRoot.querySelector('button').focus();21 expect(evtListenerMock).toHaveBeenCalled();22 });23 it('should trigger blur when button lost focus', () => {24 const element = createButton({});25 const evtListenerMock = jest.fn();26 element.addEventListener('blur', evtListenerMock);27 const btn = element.shadowRoot.querySelector('button');28 btn.focus();29 btn.blur();30 expect(evtListenerMock).toHaveBeenCalled();31 });32 it('should set title attribute on the button', () => {33 const element = createButton({ title: 'Click here' });34 const btn = element.shadowRoot.querySelector('button');35 expect(btn.getAttribute('title')).toBe('Click here');36 expect(element.getAttribute('title')).toBe(null);37 });38 it('should set aria-expanded attribute on the button when false', () => {39 const element = createButton({ ariaExpanded: 'false' });40 const btn = element.shadowRoot.querySelector('button');41 return Promise.resolve().then(() => {42 expect(btn.getAttribute('aria-expanded')).toBe('false');43 expect(element.getAttribute('aria-expanded')).toBe(null);44 });45 });46 it('should set aria-expanded attribute on the button when true', () => {47 const element = createButton({ ariaExpanded: 'true' });48 const btn = element.shadowRoot.querySelector('button');49 return Promise.resolve().then(() => {50 expect(btn.getAttribute('aria-expanded')).toBe('true');51 expect(element.getAttribute('aria-expanded')).toBe(null);52 });53 });54 it('shouldnt set aria-expanded attribute when boolean', () => {55 const element = createButton({ ariaExpanded: true });56 const btn = element.shadowRoot.querySelector('button');57 return Promise.resolve().then(() => {58 expect(btn.getAttribute('aria-expanded')).toBe(null);59 expect(element.getAttribute('aria-expanded')).toBe(null);60 });61 });62 it('should set aria-live attribute on the button', () => {63 const element = createButton({ ariaLive: 'polite' });64 const btn = element.shadowRoot.querySelector('button');65 return Promise.resolve().then(() => {66 expect(btn.getAttribute('aria-live')).toBe('polite');67 expect(element.getAttribute('aria-live')).toBe(null);68 });69 });70 it('should set aria-controls attribute on the button', () => {71 const element = createButton({ ariaControls: 'section1' });72 const btn = element.shadowRoot.querySelector('button');73 return Promise.resolve().then(() => {74 expect(element).toMatchSnapshot();75 expect(btn.getAttribute('aria-controls')).toEqual(76 expect.stringContaining('[shadow:guid]')77 );78 expect(element.getAttribute('aria-controls')).toBe(null);79 });80 });81 it('should set aria-label attribute on the button', () => {82 const element = createButton({ ariaLabel: 'btn1' });83 const btn = element.shadowRoot.querySelector('button');84 return Promise.resolve().then(() => {85 expect(btn.getAttribute('aria-label')).toBe('btn1');86 expect(element.getAttribute('aria-label')).toBe(null);87 });88 });89 it('should set aria-describedby attribute on the button', () => {90 const element = createButton({ ariaDescribedBy: 'section1' });91 const btn = element.shadowRoot.querySelector('button');92 return Promise.resolve().then(() => {93 expect(element).toMatchSnapshot();94 expect(btn.getAttribute('aria-describedby')).toEqual(95 '[shadow:guid]'96 );97 expect(element.getAttribute('aria-describedby')).toBe(null);98 });99 });100 it('aria-attributes should be present and true', () => {101 const element = createButton({102 label: 'Test',103 title: 'Click here',104 ariaExpanded: 'true',105 ariaAtomic: 'true'106 });107 const btn = element.shadowRoot.querySelector('button');108 return Promise.resolve().then(() => {109 expect(element).toMatchSnapshot();110 expect(btn.getAttribute('aria-expanded')).toBe('true');111 expect(btn.getAttribute('aria-atomic')).toBe('true');112 });113 });114 it('should set not set any aria-attributes', () => {115 const element = createButton({116 label: 'Test',117 title: 'Click here',118 tabIndex: '-1',119 ariaExpanded: null120 });121 const btn = element.shadowRoot.querySelector('button');122 return Promise.resolve().then(() => {123 expect(element).toMatchSnapshot();124 expect(btn.getAttribute('aria-expanded')).toBe(null);125 expect(btn.getAttribute('aria-atomic')).toBe(null);126 });127 });128 it('fires click event when button is clicked', () => {129 const element = createButton({ label: 'Submit' });130 const evtListenerMock = jest.fn();131 element.addEventListener('click', evtListenerMock);132 element.shadowRoot.querySelector('button').click();133 return Promise.resolve().then(() => {134 expect(evtListenerMock.mock.calls).toHaveLength(1);135 });136 });137 it('should set pointer-events to none when button is disabled', () => {138 const element = createButton({ label: 'Submit', disabled: true });139 expect(element.style.pointerEvents).toBe('none');140 element.disabled = false;141 return Promise.resolve()142 .then(() => {143 expect(element.style.pointerEvents).toBe('');144 element.disabled = true;145 })146 .then(() => {147 expect(element.style.pointerEvents).toBe('none');148 });149 });...
Using AI Code Generation
1var BestPractice = require('./BestPractice');2var bestPractice = new BestPractice();3bestPractice.evtListenerMock();4var BestPractice = function() {5 this.evtListenerMock = function() {6 var EventEmitter = require('events').EventEmitter;7 var emitter = new EventEmitter();8 emitter.on('event', function() {9 console.log('event');10 });11 emitter.emit('event');12 }13};14module.exports = BestPractice;
Using AI Code Generation
1var evtListenerMock = require('best-event-emitter').evtListenerMock;2var listener = evtListenerMock(function(err, data) {3 console.log('listener called with err: ' + err + ' and data: ' + data);4});5listener('err', 'data');6var BestEventEmitter = require('best-event-emitter');7var emitter = new BestEventEmitter();8emitter.on('test', function(err, data) {9 console.log('listener called with err: ' + err + ' and data: ' + data);10});11emitter.emit('test', 'err', 'data');12var BestEventEmitter = require('best-event-emitter');13var emitter = new BestEventEmitter();14emitter.on('test', function(err, data) {15 console.log('listener called with err: ' + err + ' and data: ' + data);16});17emitter.emit('test', 'err', 'data');18var BestEventEmitter = require('best-event-emitter');19var emitter = new BestEventEmitter();20emitter.on('test', function(err, data) {21 console.log('listener called with err: ' + err + ' and data: ' + data);22});23emitter.emit('test', 'err', 'data');24var BestEventEmitter = require('best-event-emitter');25var emitter = new BestEventEmitter();26emitter.on('test', function(err, data) {27 console.log('listener called with err: ' + err + ' and data: ' + data);28});29emitter.emit('test', 'err', 'data');30var BestEventEmitter = require('best-event-emitter');31var emitter = new BestEventEmitter();32emitter.on('test', function(err, data) {
Using AI Code Generation
1var BestEventEmitter = require('BestEventEmitter');2var evt = new BestEventEmitter();3evt.evtListenerMock('test', function() {4 console.log('test');5});6evt.emit('test');7var evt = new BestEventEmitter();8evt.evtListenerMock('test', function() {9 console.log('test');10});11evt.emit('test');12var evt = new BestEventEmitter();13evt.evtListenerMock('test', function() {14 console.log('test');15});16evt.emit('test');17var evt = new BestEventEmitter();18evt.evtListenerMock('test', function() {19 console.log('test');20});21evt.emit('test');22var evt = new BestEventEmitter();23evt.evtListenerMock('test', function() {24 console.log('test');25});26evt.emit('test');27var evt = new BestEventEmitter();28evt.evtListenerMock('test', function() {29 console.log('test');30});31evt.emit('test');32var evt = new BestEventEmitter();33evt.evtListenerMock('test', function() {34 console.log('test');35});36evt.emit('test');37var evt = new BestEventEmitter();38evt.evtListenerMock('test', function() {39 console.log('test');40});41evt.emit('test');42var evt = new BestEventEmitter();43evt.evtListenerMock('test', function() {44 console.log('test');45});46evt.emit('test');47var evt = new BestEventEmitter();48evt.evtListenerMock('test', function() {49 console.log('test');50});51evt.emit('test');52var evt = new BestEventEmitter();53evt.evtListenerMock('test', function() {54 console.log('test');55});56evt.emit('test');57var evt = new BestEventEmitter();58evt.evtListenerMock('test', function() {59 console.log('test');60});61evt.emit('test');62var evt = new BestEventEmitter();63evt.evtListenerMock('test', function() {64 console.log('test');65});66evt.emit('test');67var evt = new BestEventEmitter();68evt.evtListenerMock('test', function() {69 console.log('test');70});71evt.emit('test');72var evt = new BestEventEmitter();73evt.evtListenerMock('test', function() {74 console.log('test');75});76evt.emit('test');77var evt = new BestEventEmitter();78evt.evtListenerMock('test', function() {
Using AI Code Generation
1var BestEventEmitter = require('BestEventEmitter');2var myEmitter = new BestEventEmitter();3var evtListenerMock = myEmitter.evtListenerMock;4var evtListenerMock = myEmitter.evtListenerMock;5it('should call the event listener', function() {6 var listener = evtListenerMock();7 myEmitter.on('event', listener);8 myEmitter.emit('event');9 assert(listener.called);10});11it('should call the event listener with arguments', function() {12 var listener = evtListenerMock();13 myEmitter.on('event', listener);14 myEmitter.emit('event', 'arg1', 'arg2');15 assert(listener.calledWith('arg1', 'arg2'));16});17it('should call the event listener with arguments', function() {18 var listener = evtListenerMock();19 myEmitter.on('event', listener);20 myEmitter.emit('event', 'arg1', 'arg2');21 assert(listener.calledWith('arg1', 'arg2'));22});23it('should call the event listener with arguments', function() {24 var listener = evtListenerMock();25 myEmitter.on('event', listener);26 myEmitter.emit('event', 'arg1', 'arg2');27 assert(listener.calledWith('arg1', 'arg2'));28});29it('should call the event listener with arguments', function() {30 var listener = evtListenerMock();31 myEmitter.on('event', listener);32 myEmitter.emit('event',
Using AI Code Generation
1var BestBuyEvent = require('./BestBuyEvent.js');2var evt = BestBuyEvent.evtListenerMock();3evt.on('newProduct', function(data) {4 console.log('New product arrived at our warehouse: ' + data.name);5});6evt.emit('newProduct', { name: 'The Big Lebowski (Blu-ray)' });7var BestBuyEvent = require('./BestBuyEvent.js');8var evt = BestBuyEvent.evtListener();9evt.on('newProduct', function(data) {10 console.log('New product arrived at our warehouse: ' + data.name);11});12evt.emit('newProduct', { name: 'The Big Lebowski (Blu-ray)' });13var BestBuyEvent = require('./BestBuyEvent.js');14var evt = BestBuyEvent.evtListener();15evt.on('newProduct', function(data) {16 console.log('New product arrived at our warehouse: ' + data.name);17});18evt.emit('newProduct', { name: 'The Big Lebowski (Blu-ray)' });19var util = require('util');20var EventEmitter = require('events').EventEmitter;21var BestBuyEvent = function() {22 EventEmitter.call(this);23};24util.inherits(BestBuyEvent, EventEmitter);25BestBuyEvent.prototype.evtListenerMock = function() {26 var self = this;27 setTimeout(function() {28 self.emit('newProduct', { name: 'The Big Lebowski (Blu-ray)' });29 }, 2000);30 return this;31};32BestBuyEvent.prototype.evtListener = function() {33 var self = this;34 var request = require('request');35 if (!error && response.statusCode == 200) {36 var data = JSON.parse(body);37 console.log(data);38 self.emit('newProduct', data);39 }40 });41 return this;42};43module.exports = new BestBuyEvent();44var express = require('express');45var app = express();46var bodyParser = require('body-parser');47var http = require('http').Server(app);48var io = require('socket.io')(http);49app.use(bodyParser.json());50app.use(bodyParser.urlencoded({
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!!