Skip to content

Instantly share code, notes, and snippets.

View stalniy's full-sized avatar
🏠
Working from home

Serhii Stotskyi stalniy

🏠
Working from home
View GitHub Profile
@stalniy
stalniy / spec.js
Created June 25, 2018 18:32
Lazy vars shared validators
sharedExampleFor('Range validator', function(fieldName, min, max) {
it('rejects values greater than "max" value', function() {
$subject[fieldName] = max() + 1;
expect($subject.hasErrorOn(fieldName)).to.be.true;
});
it('rejects values less than "min" value', function() {
$subject[fieldName] = min() - 1;
expect($subject.hasErrorOn(fieldName)).to.be.true;
});
@stalniy
stalniy / spec.js
Created June 25, 2018 18:30
Lazy vars include behavior
function itBehavesLike(type) {
var args = arguments;
describe('behaves like "' + type + '"', function() {
includeExamplesFor.apply(this, args);
});
}
@stalniy
stalniy / spec.js
Created June 25, 2018 18:30
Lazy vars include shared examples
describe('Array', function() {
subject(function() {
return [];
});
includeExamplesFor('Collection');
});
describe('Set', function() {
subject(function() {
@stalniy
stalniy / spec.js
Created June 25, 2018 18:29
Lazy vars shared examples
sharedExamplesFor('Collection', function() {
it('is empty by default', function() {
expect($subject).to.be.empty;
});
it('increments "length" property when adds a new item', function() {
$subject.add(1, 2, 3);
expect($subject).to.have.length(3);
});
@stalniy
stalniy / spec.js
Created June 25, 2018 18:22
Lazy vars variable shadowing
describe('Article', function() {
subject(function() {
return Article.create({ title: 'BDD lazy vars' });
});
its('status', () => is.expected.to.equal('draft'));
describe('changes history', function() {
subject(function() {
return $subject.changes(); // $subject refers to parent subject, i.e. created article
@stalniy
stalniy / spec.js
Created June 25, 2018 18:22
Lazy vars short tests
describe('Article', function() {
subject(function() {
return Article.create({ title: 'BDD lazy vars' });
});
its('status', () => is.expected.to.equal('draft'));
// or even better
it(() => is.expected.to.be.a.draft);
});
@stalniy
stalniy / spec.js
Created June 25, 2018 18:21
Lazy vars test subject
describe('Article', function() {
subject(function() {
return Article.create({ title: 'BDD lazy vars' });
});
it('is created as a draft by default', function() {
expect($subject.status).to.equal('draft');
});
});
@stalniy
stalniy / spec.js
Created June 25, 2018 18:20
Lazy variables inside hooks
describe('Article', function() {
def('article', function() {
return new Article({ type: 'news', });
});
before(function() {
article.id = 1; // first instance
});
before(function() {
@stalniy
stalniy / spec.js
Created June 25, 2018 18:18
Write tests with lazy variables
describe('Invoice', function() {
def('user', function() {
return User.create({ role: 'member' });
});
def('invoice', function() {
return $user.invoices.create({ price: 10, currency: 'USD' });
});
describe('by default', function() {
@stalniy
stalniy / spec.js
Created June 25, 2018 18:17
How people reuse tests
describe('Invoice', function() {
var invoice, user;
describe('by default', function() {
beforeEach(function() {
user = User.create({ role: 'member' });
invoice = user.invoices.create({ price: 10, currency: 'USD' });
});
it('has status "fraud" if amount does not equal to invoice amount', function() {