Skip to content

Commit

Permalink
don't confuse similar endpoints in mockserver (#477)
Browse files Browse the repository at this point in the history
  • Loading branch information
jondayton authored Jul 11, 2023
1 parent 876516a commit 09bbaa4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
28 changes: 27 additions & 1 deletion spec/MockServer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,24 @@ class Todo extends Model {
}
}

class BlueTodo extends Model {
static type = 'blue_todos'
static endpoint = 'blue_todos'

static attributeDefinitions = {
title: {
transformer: stringType,
defaultValue: ''
},
subtitle: {
transformer: stringType,
defaultValue: ''
}
}
}

class AppStore extends Store {
static models = [Todo]
static models = [Todo, BlueTodo]
}

describe('MockServer', () => {
Expand All @@ -49,6 +65,16 @@ describe('MockServer', () => {
expect(todo.title).toEqual('Plant Seeds')
})

it('finds one with a similar type', async () => {
const mockServer = new MockServer({ factoryFarm })
mockServer.add('blue_todos', { id: '2', title: 'Plant Seeds' })
mockServer.start()

const todo = await store.fetchOne('blue_todos', '2')
expect(todo.id).toEqual('2')
expect(todo.title).toEqual('Plant Seeds')
})

it('"finds" one using a factory when record not defined in store', async () => {
const mockServer = new MockServer({ factoryFarm })

Expand Down
5 changes: 1 addition & 4 deletions src/MockServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,7 @@ class MockServer {
const { store } = _backendFactoryFarm

const { pathname } = new URL(url, 'http://example.com')
const type = Object.keys(store.data).find((model_type) => pathname.match(model_type))

let id = pathname.match(/\d+$/)
id = id && String(id)
const [, type, id] = pathname.match(/(?:\/)([A-Za-z0-9\-_]+)(?:\/*)(\d*)$/)

if (method === 'POST') {
return simulatePost(store, type, body)
Expand Down

0 comments on commit 09bbaa4

Please sign in to comment.