Skip to content

Commit a18791f

Browse files
committed
test: Coverage to switch toggle from keyboard
Refs: #39
1 parent 4ddc509 commit a18791f

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

cypress/e2e/keybord-use.spec.cy.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/// <reference types="cypress" />
2+
require('cypress-plugin-tab');
3+
import PageModel from "../support/pagemodel";
4+
import ToggleModel from "../support/togglemodel";
5+
6+
describe("Keyboard use feature", () => {
7+
context("Given ECMAS bootstrap toggle interface",()=>{
8+
testCase("ecmas");
9+
});
10+
context("Given jQuery bootstrap toggle interface",()=>{
11+
testCase("jquery");
12+
});
13+
});
14+
15+
function testCase(bstInterface) {
16+
context("When previous element is selected and tab is pressed", () => {
17+
const data_test = 'layout';
18+
it("Then toggle is focused", () => {
19+
PageModel.load(bstInterface, data_test);
20+
PageModel.getTests().each(($test) => {
21+
cy.wrap($test)
22+
.find("input.form-control")
23+
.tab().should('have.class','toggle');
24+
});
25+
});
26+
});
27+
context("When toggle element is selected and spacebar is pressed", () => {
28+
const data_test = 'layout';
29+
it.only("Then toggle is toggle", () => {
30+
PageModel.load(bstInterface, data_test);
31+
PageModel.getTests().each(($test) => {
32+
ToggleModel.checkToggleKeypress($test,true);
33+
});
34+
});
35+
});
36+
}

cypress/support/togglemodel.js

+31-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ class ToggleModel {
2424
if(isEnabled){
2525
cy.wrap(test).find(".toggle input").should("be.enabled");
2626
}else if(inputAttr == 'disabled'){
27-
cy.wrap(test).find('input').should('be.disabled').and('not.have.attr','readonly');
27+
cy.wrap(test).find('.toggle input').should('be.disabled').and('not.have.attr','readonly');
2828
}else if(inputAttr == 'readonly'){
29-
cy.wrap(test).find('input').should('be.enabled').and('have.attr','readonly');
29+
cy.wrap(test).find('.toggle input').should('be.enabled').and('have.attr','readonly');
3030
}else{
3131
throw new Error("Test fail. Argument not supported for inputAttr " + inputAttr);
3232
}
@@ -40,6 +40,35 @@ class ToggleModel {
4040
CYPRESS ISSUE 22750 (https://github.com/cypress-io/cypress/issues/22750)*/
4141
});
4242
}
43+
/**
44+
* Check a Keypress with focused toggle
45+
* @param {Chainable<JQuery<HTMLElementTagNameMap[K]>>} test : test element
46+
* @param {Boolean} isEnabled: Toggle enabled (Y/N)
47+
* @param {String} inputAttr: input attribute for disabled toggle [disabled (default) or readonly]
48+
* @static
49+
*/
50+
static checkToggleKeypress(test, isEnabled, inputAttr = 'disabled') {
51+
let prevState = test.find(".toggle input").is("[checked]");
52+
53+
if(isEnabled){
54+
cy.wrap(test).find(".toggle input").should("be.enabled");
55+
}else if(inputAttr == 'disabled'){
56+
cy.wrap(test).find('.toggle input').should('be.disabled').and('not.have.attr','readonly');
57+
}else if(inputAttr == 'readonly'){
58+
cy.wrap(test).find('.toggle input').should('be.enabled').and('have.attr','readonly');
59+
}else{
60+
throw new Error("Test fail. Argument not supported for inputAttr " + inputAttr);
61+
}
62+
63+
cy.wrap(test).find(".toggle").focus().type(' ').then(() => {
64+
let isChecked = (isEnabled && !prevState) || (!isEnabled && prevState) ;
65+
cy.wrap(test).find(".toggle input").should(isChecked ? "be.checked" : "not.be.checked");
66+
cy.wrap(test).find(".toggle").should(isChecked ? "not.have.class" : "have.class", "off");
67+
/*cy.wrap(test).find(isChecked ? '.toggle-on' : '.toggle-off').should('be.visible');
68+
cy.wrap(test).find(isChecked ? '.toggle-off' : '.toggle-on').should('not.be.visible');
69+
CYPRESS ISSUE 22750 (https://github.com/cypress-io/cypress/issues/22750)*/
70+
});
71+
}
4372

4473
/**
4574
* Check a toggle base on the toggle options

0 commit comments

Comments
 (0)