Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic cast command to #25

Merged
merged 20 commits into from
Sep 22, 2019
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ module.exports = {
"files": ["cypress/integration/**/*"],
"rules": {
"sonarjs/no-identical-functions": "warn",
"sonarjs/no-duplicate-string": "warn",
"no-invalid-this": "warn"
},
}],
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ These commands do not exist in Cypress by default.

* [`.attribute()`](./docs/attribute.md)
* [`.text()`](./docs/text.md)
* [`.toArray()`](./docs/toArray.md)

## Contributing

Expand Down
293 changes: 293 additions & 0 deletions cypress/integration/to.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,293 @@
const COMMAND_TIMEOUT = 4000;
const COMMAND_TIMEOUT_FAST = 100;

describe('The added command `to`', function() {
before(function() {
cy.visit('/');
});

beforeEach(function() {
Cypress.config('defaultCommandTimeout', COMMAND_TIMEOUT);
});

context('Input validation', function() {
let __logs;

beforeEach(function() {
__logs = [];

cy.on('log:added', (_, log) => {
__logs.push(log);
});
});

it('throws when the subject is undefined', function(done) {
cy.on('fail', (err) => {
expect(__logs.length).to.eq(2);
expect(err.message)
.to.include(`Can't cast subject of type undefined`);
done();
});

cy.wrap(undefined)
.to('string');
});

it('throws when the subject is null', function(done) {
cy.on('fail', (err) => {
expect(__logs.length).to.eq(2);
expect(err.message)
.to.include(`Can't cast subject of type null`);
done();
});

cy.wrap(null)
.to('string');
});

it('throws when the subject is NaN', function(done) {
cy.on('fail', (err) => {
expect(__logs.length).to.eq(2);
expect(err.message)
.to.include(`Can't cast subject of type NaN`);
done();
});

cy.wrap(NaN)
.to('string');
});

it('throws when a faulty value is provided for the option `log`', function(done) {
cy.on('fail', (err) => {
expect(__logs.length).to.eq(2);
expect(err.message)
.to.include('Bad value for the option "log" of the command "to".');
done();
});

cy.wrap(123)
.to('string', { log: 'foo' });
});

it('throws when a faulty type is provided', function(done) {
cy.on('fail', (err) => {
expect(__logs.length).to.eq(2);
expect(err.message)
.to.include('Can\'t cast subject to type badType.');
done();
});

cy.wrap(123).to('badType');
});

it('requires types to be case sensitive', function(done) {
cy.on('fail', (err) => {
expect(__logs.length).to.eq(2);
expect(err.message)
.to.include('Can\'t cast subject to type STRing.');
done();
});

cy.wrap(123).to('STRing');
});
});

context('String', function() {
describe('In isolation', function() {
let __logs;

beforeEach(function() {
Cypress.config('defaultCommandTimeout', COMMAND_TIMEOUT_FAST);
__logs = [];

cy.on('log:added', (_, log) => {
__logs.push(log);
});
});

it('casts a number', function() {
cy.wrap(123456)
.to('string')
.should('equal', '123456');
});

it('passes a string unmodified', function() {
cy.wrap('foo bar baz')
.to('string')
.should('equal', 'foo bar baz');
});

it('casts all items in an array', function() {
cy.wrap([132, 7, { foo: 'bar' }])
.to('string')
.should('deep.equal', ['132', '7', '{"foo":"bar"}']);
});

it('casts an object to JSON', function() {
cy.wrap({ foo: 'bar', baz: true })
.to('string')
.should('equal', '{"foo":"bar","baz":true}');
});
});
});

context('Number', function() {
describe('In isolation', function() {
let __logs;

beforeEach(function() {
Cypress.config('defaultCommandTimeout', COMMAND_TIMEOUT_FAST);
__logs = [];

cy.on('log:added', (_, log) => {
__logs.push(log);
});
});

it('casts a numberlike string', function() {
cy.wrap('007')
.to('number')
.should('equal', 7);
});

it('throws on a non-numberlike string', function(done) {
cy.on('fail', (err) => {
expect(__logs.length).to.eq(2);
expect(err.message)
.to.include(`Can't cast 'Five' to type number`);
done();
});

cy.wrap('Five')
.to('number');
});

it('throws on a non-array object', function(done) {
cy.on('fail', (err) => {
expect(__logs.length).to.eq(2);
expect(err.message)
.to.include(`Can't cast subject of type object to type number`);
done();
});

cy.wrap({ number: '123' })
.to('number');
});

it('passes a subject of type number without modification', function() {
cy.wrap(7)
.to('number')
.should('equal', 7);
});

it('casts all items in an array of numberlike strings', function() {
cy.wrap(['1234', '009', '564864'])
.to('number')
.should('deep.equal', [1234, 9, 564864]);
});

it('throws on an array containing a non-numberlike string', function(done) {
cy.on('fail', (err) => {
expect(__logs.length).to.eq(2);
expect(err.message)
.to.include(`Can't cast 'foo' to type number`);
done();
});

cy.wrap(['foo', '1234', '009', 'a125'])
.to('number');
});
});
});

context('Array', function() {
describe('In isolation', function() {
it('does not cast an Array', function() {
cy.wrap(['lorum', 'ipsum'])
.to('array')
.should('deep.equal', ['lorum', 'ipsum']);
});

it('casts a string', function() {
cy.wrap('foo')
.to('array')
.should('deep.equal', ['foo']);
});

it('casts a number', function() {
cy.wrap(123)
.to('array')
.should('deep.equal', [123]);
});

it('casts an object', function() {
cy.wrap({ foo: 123 })
.to('array')
.should('deep.equal', [{ foo: 123 }]);
});
});

describe('When interacting with page', function() {
beforeEach(function() {
cy.get('#list > *')
.as('list')
.should('have.length', 5);
});

it('does not cast a single element', function() {
// Elements are jQuery objects, which are iterable.
cy.get('@list')
.first()
.to('array')
.should((result) => {
expect(Array.isArray(result)).to.be.false;
expect(result.length).to.equal(1);
})
.each((val) => {
expect(val.jquery).to.not.be.undefined;
});
});

it('does not cast multiple elements', function() {
// Elements are jQuery objects, which are iterable.
cy.get('@list')
.to('array')
.should((result) => {
expect(Array.isArray(result)).to.be.false;
expect(result.length).to.equal(5);
})
.each((val) => {
expect(val.jquery).to.not.be.undefined;
});
});

it('does cast the text result of a single element', function() {
cy.get('@list')
.first()
.text()
.to('array')
.should((result) => {
expect(Array.isArray(result)).to.be.true;
expect(result.length).to.equal(1);
})
.each((val) => {
expect(typeof val).to.equal('string');
});
});

it('does not cast the text result of multiple elements', function() {
cy.get('@list')
.text()
.to('array')
.should((result) => {
expect(Array.isArray(result)).to.be.true;
expect(result.length).to.equal(5);
})
.each((val) => {
expect(typeof val).to.equal('string');
});
});
});
});
});

Loading