believer/movies-graphql-postgres

View on GitHub
lib/graphql/resolvers/queries/__tests__/ratings.spec.js

Summary

Maintainability
B
4 hrs
Test Coverage
const ratings = require('../ratings')
const { db } = require('../../../../adapters/db')

jest.mock('../../../../adapters/db', () => ({
  db: {
    query: jest.fn().mockReturnValue('dbData'),
  },
}))

describe('queries/ratings', () => {
  beforeEach(() => {
    db.query.mockClear()
  })

  it('should get ratings for a movie', () => {
    ratings(null, { movieId: '1' })

    expect(db.query.mock.calls[0][0]).toMatchSnapshot()
  })

  it('should return database data', async () => {
    const response = await ratings(null, { movieId: '1' })

    expect(response).toEqual('dbData')
  })

  it('should handle errors', async () => {
    db.query.mockImplementation(() => Promise.reject('nope'))

    try {
      await ratings(null, { movieId: '1' })
    } catch (e) {
      expect(e.message).toMatch(/nope/)
    }
  })
})