Skip to content

Instantly share code, notes, and snippets.

@Rishabh570
Created April 19, 2022 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rishabh570/eea76e36ad1f17cb8e9efafe1356e904 to your computer and use it in GitHub Desktop.
Save Rishabh570/eea76e36ad1f17cb8e9efafe1356e904 to your computer and use it in GitHub Desktop.
describe('Testing Item model', () => {
let sampleItemVal;
beforeEach(() => {
sampleItemVal = {
name: 'sample item',
price: 10,
rating: '5',
hash: 'hashGreaterThan10Chars'
};
});
it('it should throw an error due to missing fields', (done) => {
let item = new Item();
item.validate((err) => {
expect(err.errors.name).to.exist;
expect(err.errors.rating).to.exist;
expect(err.errors.price).to.exist;
expect(err.errors.hash).to.exist;
done();
});
});
it('it should throw an error due to incorrect hash length', (done) => {
let item = new Item(sampleItemVal);
item.validate((err) => {
if (err) {
expect(err).to.be.instanceOf(ValidationError);
// this is expected, do not pass err to done()
done();
} else {
const unexpectedSuccessError = new Error('⚠️ Unexpected success!');
done(unexpectedSuccessError);
}
});
});
it('it should create the item successfully with correct parameters', (done) => {
let item = new Item({
...sampleItemVal,
hash: '1234567891'
});
item.validate((err) => {
if (err) {
const unexpectedFailureError = new Error('⚠️ Unexpected failure!');
done(unexpectedFailureError);
} else {
expect(item.hash).to.equal('1234567891');
done();
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment