Skip to content

Commit

Permalink
test(effects): add tests to cover effects
Browse files Browse the repository at this point in the history
  • Loading branch information
simplymichael committed Dec 11, 2023
1 parent 6ea7719 commit e939efa
Show file tree
Hide file tree
Showing 6 changed files with 409 additions and 1 deletion.
17 changes: 17 additions & 0 deletions __tests__/effects.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const { describe } = require("./setup");
const { getTestDirectory, getTestFile, getTestFiles, singleFileTest } = require("./test-helpers");

const [testSingleMethod, testFile] = getTestFile();
const testDirectory = getTestDirectory(__filename);
const testFiles = getTestFiles(__dirname, testDirectory);


module.exports = function validatorsTest() {
describe("effects", function() {
if(testSingleMethod) {
singleFileTest(__dirname, testDirectory, testFile);
} else {
testFiles.forEach(testFile => singleFileTest(__dirname, testDirectory, testFile));
}
});
};
138 changes: 138 additions & 0 deletions __tests__/effects/addBottomBorder.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/* eslint-env node, mocha */

const { describe } = require("mocha");
const bottomBorderEffect = require("../../src/effects/add-bottom-border");
const { VALID_FIELD_CLASSNAME, INVALID_FIELD_CLASSNAME } = require("../setup");

const classListMethods = {
add(key, val) {
this[key] = val;
},
contains(key) {
return typeof this[key] !== "undefined";
}
};

module.exports = {
arguments: [],
test: addBottomBorder,
};

function addBottomBorder(it, expect) {
beforeEach(function(done) {
const formId = "signup-form";
const form = $(`#${formId}`); // eslint-disable-line
const formElements = form.elements;
const submitBtn = $("[role='submit-button']"); // eslint-disable-line

this.currentTest.formId = formId;
this.currentTest.form = form;
this.currentTest.formElements = formElements;
this.currentTest.submitBtn = submitBtn;

done();
});

it("should be an object", function() {
expect(bottomBorderEffect).to.be.an("object");
expect(bottomBorderEffect).to.have.property("name", "addBottomBorder");
expect(bottomBorderEffect).to.have.property("meta");
expect(bottomBorderEffect).to.have.property("valid");
expect(bottomBorderEffect).to.have.property("invalid");
expect(bottomBorderEffect.meta).to.be.an("object");
expect(bottomBorderEffect.valid).to.be.a("function");
expect(bottomBorderEffect.invalid).to.be.a("function");
});

describe(".valid(field)", function() {
it("should take no action if passed a non-object", function() {
const testArray = new Array();
const testFunction = function testFunction() {};

testArray.classList = {
add: classListMethods.add.bind(testArray),
contains: classListMethods.contains.bind(testArray),
};

testFunction.classList = {
add: classListMethods.add.bind(testFunction),
contains: classListMethods.contains.bind(testFunction),
};

const fields = [testArray, testFunction];

fields.forEach(function assertFieldIgnored(field) {
bottomBorderEffect.valid(field);

expect(field.classList.contains(VALID_FIELD_CLASSNAME)).to.equal(false);
expect(field.classList.contains(INVALID_FIELD_CLASSNAME)).to.equal(false);
});
});

it("should take no action on a submit button", function() {
const btn = this.test.submitBtn;

bottomBorderEffect.valid(btn);

expect(btn.classList.contains(VALID_FIELD_CLASSNAME)).to.equal(false);
expect(btn.classList.contains(INVALID_FIELD_CLASSNAME)).to.equal(false);
});

it("should add the appropriate classnames to the field", function() {
Array.from(this.test.formElements).forEach(function assertValidOnElement(element) {
if(["name-field", "username-field", "email-field"].includes(element.id)) {
bottomBorderEffect.valid(element);

expect(element.classList.contains(VALID_FIELD_CLASSNAME)).to.equal(true);
expect(element.classList.contains(INVALID_FIELD_CLASSNAME)).to.equal(false);
}
});
});
});

describe(".invalid(field)", function() {
it("should take no action if passed a non-object", function() {
const testArray = new Array();
const testFunction = function testFunction() {};

testArray.classList = {
add: classListMethods.add.bind(testArray),
contains: classListMethods.contains.bind(testArray),
};

testFunction.classList = {
add: classListMethods.add.bind(testFunction),
contains: classListMethods.contains.bind(testFunction),
};

const fields = [testArray, testFunction];

fields.forEach(function assertFieldIgnored(field) {
bottomBorderEffect.invalid(field);

expect(field.classList.contains(VALID_FIELD_CLASSNAME)).to.equal(false);
expect(field.classList.contains(INVALID_FIELD_CLASSNAME)).to.equal(false);
});
});

it("should take no action on a submit button", function() {
const btn = this.test.submitBtn;

bottomBorderEffect.invalid(btn);

expect(btn.classList.contains(VALID_FIELD_CLASSNAME)).to.equal(false);
expect(btn.classList.contains(INVALID_FIELD_CLASSNAME)).to.equal(false);
});

it("should add the appropriate classnames to the field", function() {
Array.from(this.test.formElements).forEach(function assertValidOnElement(element) {
if(["name-field", "username-field", "email-field"].includes(element.id)) {
bottomBorderEffect.invalid(element);

expect(element.classList.contains(VALID_FIELD_CLASSNAME)).to.equal(false);
expect(element.classList.contains(INVALID_FIELD_CLASSNAME)).to.equal(true);
}
});
});
});
}
138 changes: 138 additions & 0 deletions __tests__/effects/addValidationStatusIcons.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/* eslint-env node, mocha */

const { describe } = require("mocha");
const validationStatusIconsEffect = require("../../src/effects/add-validation-status-icons");
const { VALID_FIELD_CLASSNAME, INVALID_FIELD_CLASSNAME } = require("../setup");

const classListMethods = {
add(key, val) {
this[key] = val;
},
contains(key) {
return typeof this[key] !== "undefined";
}
};

module.exports = {
arguments: [],
test: addValidationStatusIcons,
};

function addValidationStatusIcons(it, expect) {
beforeEach(function(done) {
const formId = "signup-form";
const form = $(`#${formId}`); // eslint-disable-line
const formElements = form.elements;
const submitBtn = $("[role='submit-button']"); // eslint-disable-line

this.currentTest.formId = formId;
this.currentTest.form = form;
this.currentTest.formElements = formElements;
this.currentTest.submitBtn = submitBtn;

done();
});

it("should be an object", function() {
expect(validationStatusIconsEffect).to.be.an("object");
expect(validationStatusIconsEffect).to.have.property("name", "addValidationStatusIcons");
expect(validationStatusIconsEffect).to.have.property("meta");
expect(validationStatusIconsEffect).to.have.property("valid");
expect(validationStatusIconsEffect).to.have.property("invalid");
expect(validationStatusIconsEffect.meta).to.be.an("object");
expect(validationStatusIconsEffect.valid).to.be.a("function");
expect(validationStatusIconsEffect.invalid).to.be.a("function");
});

describe(".valid(field)", function() {
it("should take no action if passed a non-object", function() {
const testArray = new Array();
const testFunction = function testFunction() {};

testArray.classList = {
add: classListMethods.add.bind(testArray),
contains: classListMethods.contains.bind(testArray),
};

testFunction.classList = {
add: classListMethods.add.bind(testFunction),
contains: classListMethods.contains.bind(testFunction),
};

const fields = [testArray, testFunction];

fields.forEach(function assertFieldIgnored(field) {
validationStatusIconsEffect.valid(field);

expect(field.classList.contains(VALID_FIELD_CLASSNAME)).to.equal(false);
expect(field.classList.contains(INVALID_FIELD_CLASSNAME)).to.equal(false);
});
});

it("should take no action on a submit button", function() {
const btn = this.test.submitBtn;

validationStatusIconsEffect.valid(btn);

expect(btn.classList.contains(VALID_FIELD_CLASSNAME)).to.equal(false);
expect(btn.classList.contains(INVALID_FIELD_CLASSNAME)).to.equal(false);
});

it("should add the appropriate classnames to the field", function() {
Array.from(this.test.formElements).forEach(function assertValidOnElement(element) {
if(["name-field", "username-field", "email-field"].includes(element.id)) {
validationStatusIconsEffect.valid(element);

expect(element.classList.contains(VALID_FIELD_CLASSNAME)).to.equal(true);
expect(element.classList.contains(INVALID_FIELD_CLASSNAME)).to.equal(false);
}
});
});
});

describe(".invalid(field)", function() {
it("should take no action if passed a non-object", function() {
const testArray = new Array();
const testFunction = function testFunction() {};

testArray.classList = {
add: classListMethods.add.bind(testArray),
contains: classListMethods.contains.bind(testArray),
};

testFunction.classList = {
add: classListMethods.add.bind(testFunction),
contains: classListMethods.contains.bind(testFunction),
};

const fields = [testArray, testFunction];

fields.forEach(function assertFieldIgnored(field) {
validationStatusIconsEffect.invalid(field);

expect(field.classList.contains(VALID_FIELD_CLASSNAME)).to.equal(false);
expect(field.classList.contains(INVALID_FIELD_CLASSNAME)).to.equal(false);
});
});

it("should take no action on a submit button", function() {
const btn = this.test.submitBtn;

validationStatusIconsEffect.invalid(btn);

expect(btn.classList.contains(VALID_FIELD_CLASSNAME)).to.equal(false);
expect(btn.classList.contains(INVALID_FIELD_CLASSNAME)).to.equal(false);
});

it("should add the appropriate classnames to the field", function() {
Array.from(this.test.formElements).forEach(function assertValidOnElement(element) {
if(["name-field", "username-field", "email-field"].includes(element.id)) {
validationStatusIconsEffect.invalid(element);

expect(element.classList.contains(VALID_FIELD_CLASSNAME)).to.equal(false);
expect(element.classList.contains(INVALID_FIELD_CLASSNAME)).to.equal(true);
}
});
});
});
}
Loading

0 comments on commit e939efa

Please sign in to comment.