Skip to content

Instantly share code, notes, and snippets.

@Rishabh570
Created April 19, 2022 18:35
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/b9ac322047ff10b2c7d1858bea307d21 to your computer and use it in GitHub Desktop.
Save Rishabh570/b9ac322047ff10b2c7d1858bea307d21 to your computer and use it in GitHub Desktop.
describe('Testing express app routes', () => {
afterEach(() => {
app = rewire('../app');
sandbox.restore();
});
describe('Testing /item route', () => {
let sampleItemVal, hash;
beforeEach(() => {
hash = '1234567891';
sampleItemVal = {
name: 'sample item',
price: 10,
rating: '5',
hash
};
sandbox.stub(itemController, 'readItem').resolves(sampleItemVal);
sandbox.stub(itemController, 'createItem').resolves(sampleItemVal);
sandbox.stub(itemController, 'updateItemHash').resolves(sampleItemVal);
});
it('GET /:hash should successfully return item', (done) => {
request(app)
.get(`/item/${hash}`)
.expect(200)
.end((err, response) => {
expect(response.body).to.have.property('message').to.equal('Item read successfully!');
expect(response.body)
.to.have.property('item')
.to.have.property('name')
.to.equal('sample item');
expect(response.body).to.have.property('item').to.have.property('price').to.equal(10);
expect(response.body).to.have.property('item').to.have.property('rating').to.equal('5');
expect(response.body).to.have.property('item').to.have.property('hash').to.equal(hash);
done(err); // err is null in success scenario
});
});
it('POST / should successfully create a new item', (done) => {
request(app)
.post('/item/')
.send(sampleItemVal)
.expect(200)
.end((err, response) => {
expect(response.body).to.have.property('message').to.equal('Item created successfully!');
expect(response.body)
.to.have.property('item')
.to.have.property('name')
.to.equal('sample item');
expect(response.body).to.have.property('item').to.have.property('price').to.equal(10);
expect(response.body).to.have.property('item').to.have.property('rating').to.equal('5');
expect(response.body).to.have.property('item').to.have.property('hash').to.equal(hash);
done(err);
});
});
it('PUT / should successfully update hash for a given item', (done) => {
request(app)
.put('/item')
.send(hash)
.expect(200)
.end((err, response) => {
expect(response.body).to.have.property('message').to.equal('Item updated successfully!');
expect(response.body)
.to.have.property('item')
.to.have.property('name')
.to.equal('sample item');
expect(response.body).to.have.property('item').to.have.property('price').to.equal(10);
expect(response.body).to.have.property('item').to.have.property('rating').to.equal('5');
expect(response.body).to.have.property('item').to.have.property('hash').to.equal(hash);
done(err);
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment