Skip to content

Commit

Permalink
Migrated tests to Typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
geclos committed May 20, 2019
1 parent 2b2252c commit 03fd8cb
Show file tree
Hide file tree
Showing 23 changed files with 1,719 additions and 2,708 deletions.
16 changes: 3 additions & 13 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
parser: 'babel-eslint',
plugins: [ 'flowtype' ],
parser: '@typescript-eslint/parser',
plugins: [ '@typescript-eslint' ],

env: {
browser: true,
Expand All @@ -18,20 +18,10 @@ module.exports = {
}
},

extends: [
'plugin:flowtype/recommended',
'standard'
],

rules: {
'object-curly-spacing': ['warn', 'always'],
'prefer-promise-reject-errors': 'off',
'no-duplicate-imports': 'off',
'flowtype/no-weak-types': [0, {
'any': false,
'Object': false,
'Function': false
}]
'no-duplicate-imports': 'off'
}
};

3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
- [x] Documented new APIs
- [ ] Update the mobx-rest-example
- [x] Fix https://github.com/masylum/mobx-rest/issues/47
- [x] Add support for mobx4
- [x] Migrate to mobx 5+
- [x] Fix https://github.com/masylum/mobx-rest/issues/41
- [x] Migrate to typescript
- [x] Deprecated `.peek()` Collection method

## `3.0.0.alpha`

Expand Down
File renamed without changes.
31 changes: 10 additions & 21 deletions __tests__/Collection.spec.js → __tests__/Collection.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import Model from '../src/Model'
import Collection from '../src/Collection'
import apiClient from '../src/apiClient'
import MockApi from './mocks/api'
import Model from '../src/Model'
import apiClient from '../src/apiClient'

apiClient(MockApi)

class MockCollection extends Collection<Model> {
model (): typeof Model {
return Model
}
}

describe(Collection, () => {
let collection

beforeEach(() => {
collection = new Collection([
collection = new MockCollection([
{ id: 1, phone: '1234' },
{ id: 2, phone: '5678' }
])
Expand Down Expand Up @@ -74,17 +80,6 @@ describe(Collection, () => {
})
})

describe('peek()', () => {
it('returns a performant shallow copy of the models array', () => {
const models = collection.peek()

models.pop()

expect(models.length).toBe(1)
expect(collection.models.length).toBe(1)
})
})

describe('model()', () => {
it('returns the default model class', () => {
expect(collection.model()).toBe(Model)
Expand Down Expand Up @@ -518,10 +513,6 @@ describe(Collection, () => {
jest.spyOn(apiClient(), 'post')
})

afterEach(() => {
apiClient().post.mockRestore()
})

it('builds a model and saves it', async () => {
const attributes = { phone: '1234' }
const promise = collection.create(attributes)
Expand Down Expand Up @@ -603,9 +594,7 @@ describe(Collection, () => {
promise = collection.fetch(options)
})

afterEach(() => {
apiClient().get.mockRestore()
})
afterEach(() => spy.mockRestore())

it('makes a get request to the model url', () => {
expect(spy).toHaveBeenCalled()
Expand Down
File renamed without changes.
90 changes: 36 additions & 54 deletions __tests__/Model.spec.js → __tests__/Model.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { isObservable } from 'mobx'
import Model from '../src/Model'
import Collection from '../src/Collection'
import apiClient from '../src/apiClient'
import MockApi from './mocks/api'
import Model from '../src/Model'
import apiClient from '../src/apiClient'
import { isObservable } from 'mobx'
import { strMapToObj } from './utils'

apiClient(MockApi)

class MockCollection extends Collection<Model> {
model (): typeof Model {
return Model
}
}

describe(Model, () => {
class MyModel extends Model {
urlRoot = () => '/resources'
Expand All @@ -29,24 +36,17 @@ describe(Model, () => {
lastName: 'Doe'
})

expect(model.committedAttributes.toJS()).toEqual({
expect(strMapToObj(model.committedAttributes.toJS())).toEqual({
firstName: 'John',
lastName: 'Doe'
})
})

it('allows to define default attributes', () => {
class MyModel extends Model {
static defaultAttributes = {
email: null,
phone: null
}
}

const model = new MyModel({
const model = new Model({
firstName: 'John',
lastName: 'Doe'
})
}, { email: null, phone: null })

expect(model.toJS()).toEqual({
firstName: 'John',
Expand Down Expand Up @@ -92,7 +92,7 @@ describe(Model, () => {
describe('if the model belongs to a collection and urlRoot is not defined', () => {
it('uses the collection url as root', () => {
const model = new Model({ id: 2 })
const collection = new Collection()
const collection = new MockCollection()

collection.url = () => '/different-resources'
model.collection = collection
Expand Down Expand Up @@ -376,13 +376,7 @@ describe(Model, () => {
})

it('respects the default attributes', () => {
class MyModel extends Model {
static defaultAttributes = {
someAttribute: 'test'
}
}

const model = new MyModel({ name: 'john' })
const model = new Model({ name: 'john' }, { someAttribute: 'test' })

model.reset({ phone: '1234567' })

Expand All @@ -395,12 +389,7 @@ describe(Model, () => {

describe('if attributes is not specified', () => {
it('replaces the current attributes with the default ones', () => {
class MyModel extends Model {
static defaultAttributes = {
someAttribute: 'test'
}
}
const model = new MyModel({ email: 'test@test.com' })
const model = new Model({ email: 'test@test.com' }, { someAttribute: 'test' })

model.reset()

Expand Down Expand Up @@ -446,9 +435,7 @@ describe(Model, () => {
})
})

afterEach(() => {
apiClient().get.mockRestore()
})
afterEach(() => spy.mockRestore())

it('makes a get request to the model url', () => {
expect(spy).toHaveBeenCalled()
Expand Down Expand Up @@ -478,8 +465,13 @@ describe(Model, () => {

it('sets the new attributes as committed', async () => {
MockApi.resolvePromise({ id: 2, name: 'John' })

await promise
expect(model.committedAttributes.toJS()).toEqual({ id: 2, name: 'John' })

expect(strMapToObj(model.committedAttributes.toJS())).toEqual({
id: 2,
name: 'John'
})
})
})

Expand Down Expand Up @@ -507,9 +499,7 @@ describe(Model, () => {
spy = jest.spyOn(apiClient(), 'post')
})

afterEach(() => {
apiClient().post.mockRestore()
})
afterEach(() => spy.mockRestore())

it('sends a POST request', () => {
model.save()
Expand Down Expand Up @@ -565,9 +555,7 @@ describe(Model, () => {
spy = jest.spyOn(apiClient(), 'patch')
})

afterEach(() => {
apiClient().patch.mockRestore()
})
afterEach(() => spy.mockRestore())

it('sends a PATCH request', () => {
model.save({}, { patch: true })
Expand Down Expand Up @@ -616,9 +604,7 @@ describe(Model, () => {
spy = jest.spyOn(apiClient(), 'put')
})

afterEach(() => {
apiClient().put.mockRestore()
})
afterEach(() => spy.mockRestore())

it('sends a PUT request', () => {
model.save()
Expand Down Expand Up @@ -673,9 +659,7 @@ describe(Model, () => {
spy = jest.spyOn(apiClient(), 'post')
})

afterEach(() => {
apiClient().post.mockRestore()
})
afterEach(() => spy.mockRestore())

it('they must be passed to the adapter', () => {
model.save(undefined, { method: 'HEAD' })
Expand Down Expand Up @@ -717,7 +701,7 @@ describe(Model, () => {
it('sets the new attributes as committed', async () => {
const promise = model.save({ phone: '5678' })

expect(model.committedAttributes.toJS()).toEqual({
expect(strMapToObj(model.committedAttributes.toJS())).toEqual({
name: 'John',
email: 'john@test.com',
phone: '1234'
Expand All @@ -732,7 +716,7 @@ describe(Model, () => {

await promise

expect(model.committedAttributes.toJS()).toEqual({
expect(strMapToObj(model.committedAttributes.toJS())).toEqual({
id: 2,
name: 'John',
email: 'john@test.com',
Expand Down Expand Up @@ -940,9 +924,7 @@ describe(Model, () => {
spy = jest.spyOn(apiClient(), 'del')
})

afterEach(() => {
apiClient().del.mockRestore()
})
afterEach(() => spy.mockRestore())

describe('if is new', () => {
it('don\'t make any request', () => {
Expand All @@ -954,7 +936,7 @@ describe(Model, () => {
let collection

beforeEach(() => {
collection = new Collection()
collection = new MockCollection()
model.collection = collection
collection.models.push(model)
})
Expand Down Expand Up @@ -982,7 +964,7 @@ describe(Model, () => {
let collection

beforeEach(() => {
collection = new Collection()
collection = new MockCollection()
model.collection = collection
collection.models.push(model)
})
Expand All @@ -997,7 +979,7 @@ describe(Model, () => {
describe('if the request succeds', () => {
describe('if not optimistic and belongs to a collection', () => {
it('removes itself from the collection', async () => {
const collection = new Collection()
const collection = new MockCollection()

model.collection = collection
collection.models.push(model)
Expand All @@ -1015,7 +997,7 @@ describe(Model, () => {

describe('if optimistic or don\'t belongs to a collection', () => {
it('not throws', async () => {
model.collection = new Collection()
model.collection = new MockCollection()
model.collection.models.push(model)

const promise = model.destroy({ optimistic: true })
Expand All @@ -1040,7 +1022,7 @@ describe(Model, () => {
describe('if the request fails', () => {
describe('if optimistic and belongs to a collection', () => {
it('adds itself to the collection again', async () => {
const collection = new Collection()
const collection = new MockCollection()

model.collection = collection
collection.models.push(model)
Expand All @@ -1061,7 +1043,7 @@ describe(Model, () => {

describe('if not optimistic or don\'t belongs to a collection', () => {
it('throws the request response', async () => {
model.collection = new Collection()
model.collection = new MockCollection()
model.collection.models.push(model)

const promise = model.destroy({ optimistic: false })
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions __tests__/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function strMapToObj(strMap) {
let obj = Object.create(null);
for (let [k,v] of strMap) {
// We don’t escape the key '__proto__'
// which can cause problems on older engines
obj[k] = v;
}
return obj;
}
Loading

0 comments on commit 03fd8cb

Please sign in to comment.