diff --git a/spec/MockServer.spec.ts b/spec/MockServer.spec.ts index e24360f7..fbad461f 100644 --- a/spec/MockServer.spec.ts +++ b/spec/MockServer.spec.ts @@ -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', () => { @@ -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 }) diff --git a/src/MockServer.ts b/src/MockServer.ts index cfdd01c8..f8b3d80f 100644 --- a/src/MockServer.ts +++ b/src/MockServer.ts @@ -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)