JS: Stub a method to do unit test via testdouble

269 Views Asked by At

I'm trying to 'stub' a method via testdoubleJS to do a unit test for this method (doing npm test). It is the first time I'm doing this, so it is still hard to understand for me. For my attempt - shown below - I do get the error TypeError: mediaAddImagePoint.run is not a function

This is how my method I want to test looks like:

import { ValidatedMethod } from 'meteor/mdg:validated-method'
import { LoggedInMixin } from 'meteor/tunifight:loggedin-mixin'
import { Media } from '/imports/api/media/collection.js'

const mediaAddImagePoint = new ValidatedMethod({
  name: 'media.point.add',
  mixins: [LoggedInMixin],
  checkLoggedInError: { error: 'notLogged' },
  validate: null,

  run ({ id, x, y }) {
    Media.update(
      { _id: id },
      {
        $push: {
          'meta.points': {
            id: Random.id(),
            x,
            y
          }
        }
      }
    )
  }
})

And this is how I'm trying to test this method via testdouble:

import { expect } from 'chai'
import td from 'testdouble'

describe('media.point.add', function () {
  describe('mediaAddImagePoint', function () {
    let Media = td.object(['update'])
    let ValidatedMethod = td.function()
    let LoggedInMixin = td.function()
    let mediaAddImagePoint

    beforeEach(function () {
      td.replace('meteor/mdg:validated-method', { ValidatedMethod })
      td.replace('meteor/tunifight:loggedin-mixin', { LoggedInMixin })
      td.replace('/imports/api/media/collection.js', { Media })
      mediaAddImagePoint = require('../../imports/api/media/methods/imagePoints.js').mediaAddImagePoint
    })

    afterEach(function () {
      td.reset()
    })

    it('should add image point', function () {
      const query = { id: 'sampleID', x: 12, y: 34 }
      mediaAddImagePoint.run(query)
      td.verify(Media.update(query))
    })
  })
})
0

There are 0 best solutions below