{"size": 8911, "ext": "js", "max_stars_repo_path": "__tests__/adapter.spec.js", "max_stars_repo_name": "masylum/mobx-rest-jquery-adapter", "max_stars_repo_stars_event_min_datetime": "2017-06-05T17:11:53.000Z", "max_stars_repo_stars_event_max_datetime": "2017-09-19T12:51:53.000Z", "max_issues_repo_path": "__tests__/adapter.spec.js", "max_issues_repo_name": "masylum/mobx-rest-jquery-adapter", "max_issues_repo_issues_event_min_datetime": "2018-04-26T08:33:37.000Z", "max_issues_repo_issues_event_max_datetime": "2021-09-01T05:05:20.000Z", "max_forks_repo_path": "__tests__/adapter.spec.js", "max_forks_repo_name": "masylum/mobx-rest-jquery-adapter", "max_forks_repo_forks_event_min_datetime": "2017-09-18T12:31:40.000Z", "max_forks_repo_forks_event_max_datetime": "2019-05-10T11:06:29.000Z"}
{"max_stars_count": 2, "max_issues_count": 3, "max_forks_count": 3, "avg_line_length": 24.4807692308, "max_line_length": 73, "alphanum_fraction": 0.4888340254}
import adapter from '../src'
import jq from 'jquery'

adapter.apiPath = '/api'
adapter.commonOptions = {
  headers: { 'SomeHeader': 'test' },
  xhrFields: { withCredentials: true }
}

const noop = () => {}

const injectDone = (values) => {
  jq.ajax = jest.genMockFunction().mockImplementation((url, options) => {
    return {
      done: (cb) => {
        cb(values)
        return { fail: noop }
      }
    }
  })
}

const injectFail = (values) => {
  jq.ajax = jest.genMockFunction().mockImplementation((url, options) => {
    return {
      done: () => {
        return {
          fail: (cb) => {
            cb({ responseText: values })
          }
        }
      }
    }
  })
}

describe('adapter', () => {
  describe('ajax', () => {
    describe('when it fails with a malformed response', () => {
      let ret
      const values = 'ERROR'

      beforeEach(() => {
        injectFail(values)
        ret = adapter.get('/users')
      })

      it('returns the error wrapper into an array', () => {
        expect(ret.abort).toBeTruthy()

        return ret.promise.catch((vals) => {
          expect(vals).toEqual({})
        })
      })
    })
  })

  describe('get', () => {
    let ret
    const data = { manager_id: 2 }

    const action = () => {
      ret = adapter.get('/users', data)
    }

    describe('when it resolves', () => {
      const values = { id: 1, name: 'paco' }

      beforeEach(() => {
        injectDone(values)
        action()
      })

      it('sends a xhr request with data parameters', () => {
        expect(ret.abort).toBeTruthy()

        return ret.promise.then((vals) => {
          expect(vals).toEqual(values)
          expect(jq.ajax.mock.calls[0][0]).toBe('/api/users')
          expect(jq.ajax.mock.calls[0][1]).toEqual({
            data,
            headers: { 'SomeHeader': 'test' },
            xhrFields: { withCredentials: true }
          })
        })
      })
    })

    describe('when it fails', () => {
      const values = '{"errors": ["foo"]}'

      beforeEach(() => {
        injectFail(values)
        action()
      })

      it('sends a xhr request with data parameters', () => {
        expect(ret.abort).toBeTruthy()

        return ret.promise.catch((vals) => {
          expect(vals).toEqual(['foo'])
        })
      })
    })
  })

  describe('post', () => {
    let ret
    let data

    const action = () => {
      ret = adapter.post('/users', data)
    }

    describe('when it resolves', () => {
      const values = { id: 1, name: 'paco' }

      beforeEach(() => {
        data = { name: 'paco' }
        injectDone(values)
        action()
      })

      it('sends a xhr request with data parameters', () => {
        expect(ret.abort).toBeTruthy()

        return ret.promise.then((vals) => {
          expect(vals).toEqual(values)
          expect(jq.ajax.mock.calls[0][0]).toBe('/api/users')
          expect(jq.ajax.mock.calls[0][1]).toEqual({
            method: 'POST',
            contentType: 'application/json',
            headers: { 'SomeHeader': 'test' },
            xhrFields: { withCredentials: true },
            data: '{"name":"paco"}'
          })
        })
      })
    })

    describe('when it fails', () => {
      const values = '{"errors": ["foo"]}'

      beforeEach(() => {
        data = { name: 'paco' }
        injectFail(values)
        action()
      })

      it('sends a xhr request with data parameters', () => {
        expect(ret.abort).toBeTruthy()

        return ret.promise.catch((vals) => {
          expect(vals).toEqual(['foo'])
        })
      })
    })

    describe('when it contains a file', () => {
      const values = { id: 1, avatar: 'lol.png' }

      beforeEach(() => {
        data = { avatar: new File([''], 'filename') }
        injectDone(values)
        action()
      })

      it('sends a xhr request with data parameters', () => {
        expect(ret.abort).toBeTruthy()

        return ret.promise.then((vals) => {
          const res = jq.ajax.mock.calls[0][1]

          expect(vals).toEqual(values)
          expect(jq.ajax.mock.calls[0][0]).toBe('/api/users')
          expect(res.cache).toBe(false)
          expect(res.contentType).toBe(false)
          expect(res.data).toBeTruthy()
          expect(res.method).toBe('POST')
          expect(res.processData).toBe(false)
          expect(res.xhr).toBeTruthy()
        })
      })
    })

    describe('when it contains an array of files', () => {
      const values = [{ id: 1, avatar: 'lol.png' }]

      beforeEach(() => {
        data = { files: [new File([''], 'filename')] }
        injectDone(values)
        action()
      })

      it('sends a xhr request with data parameters', () => {
        expect(ret.abort).toBeTruthy()

        return ret.promise.then((vals) => {
          const res = jq.ajax.mock.calls[0][1]

          expect(vals).toEqual(values)
          expect(jq.ajax.mock.calls[0][0]).toBe('/api/users')
          expect(res.cache).toBe(false)
          expect(res.contentType).toBe(false)
          expect(res.data).toBeTruthy()
          expect(res.method).toBe('POST')
          expect(res.processData).toBe(false)
          expect(res.xhr).toBeTruthy()
        })
      })
    })
  })

  describe('put', () => {
    let ret
    const data = { name: 'paco' }

    const action = () => {
      ret = adapter.put('/users', data)
    }

    describe('when it resolves', () => {
      const values = { id: 1, name: 'paco' }

      beforeEach(() => {
        injectDone(values)
        action()
      })

      it('sends a xhr request with data parameters', () => {
        expect(ret.abort).toBeTruthy()

        return ret.promise.then((vals) => {
          expect(vals).toEqual(values)
          expect(jq.ajax.mock.calls[0][0]).toBe('/api/users')
          expect(jq.ajax.mock.calls[0][1]).toEqual({
            method: 'PUT',
            contentType: 'application/json',
            headers: { 'SomeHeader': 'test' },
            xhrFields: { withCredentials: true },
            data: '{"name":"paco"}'
          })
        })
      })
    })

    describe('when it fails', () => {
      const values = '{"errors": ["foo"]}'

      beforeEach(() => {
        injectFail(values)
        action()
      })

      it('sends a xhr request with data parameters', () => {
        expect(ret.abort).toBeTruthy()

        return ret.promise.catch((vals) => {
          expect(vals).toEqual(['foo'])
        })
      })
    })
  })

  describe('patch', () => {
    let ret
    const data = { name: 'paco' }

    const action = () => {
      ret = adapter.patch('/users', data)
    }

    describe('when it resolves', () => {
      const values = { id: 1, name: 'paco' }

      beforeEach(() => {
        injectDone(values)
        action()
      })

      it('sends a xhr request with data parameters', () => {
        expect(ret.abort).toBeTruthy()

        return ret.promise.then((vals) => {
          expect(vals).toEqual(values)
          expect(jq.ajax.mock.calls[0][0]).toBe('/api/users')
          expect(jq.ajax.mock.calls[0][1]).toEqual({
            method: 'PATCH',
            contentType: 'application/json',
            headers: { 'SomeHeader': 'test' },
            xhrFields: { withCredentials: true },
            data: '{"name":"paco"}'
          })
        })
      })
    })

    describe('when it fails', () => {
      const values = '{"errors": ["foo"]}'

      beforeEach(() => {
        injectFail(values)
        action()
      })

      it('sends a xhr request with data parameters', () => {
        expect(ret.abort).toBeTruthy()

        return ret.promise.catch((vals) => {
          expect(vals).toEqual(['foo'])
        })
      })
    })
  })

  describe('del', () => {
    let ret

    const action = () => { ret = adapter.del('/users', { foo: 'bar' }) }

    describe('when it resolves', () => {
      const values = { id: 1, name: 'paco' }

      beforeEach(() => {
        injectDone(values)
        action()
      })

      it('sends a xhr request with data parameters', () => {
        expect(ret.abort).toBeTruthy()

        return ret.promise.then((vals) => {
          expect(vals).toEqual(values)
          expect(jq.ajax.mock.calls[0][0]).toBe('/api/users')
          expect(jq.ajax.mock.calls[0][1]).toEqual({
            method: 'DELETE',
            contentType: 'application/json',
            headers: { 'SomeHeader': 'test' },
            xhrFields: { withCredentials: true },
            data: '{"foo":"bar"}'
          })
        })
      })
    })

    describe('when it fails', () => {
      const values = '{"errors": ["foo"]}'

      beforeEach(() => {
        injectFail(values)
        action()
      })

      it('sends a xhr request with data parameters', () => {
        expect(ret.abort).toBeTruthy()

        return ret.promise.catch((vals) => {
          expect(vals).toEqual(['foo'])
        })
      })
    })
  })
})
