Skip to content

Commit 0b46b2b

Browse files
authored
Ensure select and selectExtra are mergeable (#103)
1 parent cb8e9ce commit 0b46b2b

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

src/scope.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@ export class Scope<T extends typeof JSORMBase = typeof JSORMBase> {
258258
queryParams.filter[key] = associationQueryParams.filter
259259
queryParams.stats[key] = associationQueryParams.stats
260260

261+
Object.assign(queryParams.fields, associationQueryParams.fields)
262+
Object.assign(queryParams.extra_fields, associationQueryParams.extra_fields)
263+
261264
associationQueryParams.sort.forEach(s => {
262265
const transformed = this._transformAssociationSortParam(key, s)
263266
queryParams.sort.push(transformed)

test/integration/finders.test.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,50 @@ describe("Model finders", () => {
374374
expect(data[0]).to.be.instanceof(Person)
375375
expect(data[0]).to.have.property("id", "2")
376376
})
377+
378+
describe("when merging association #select", () => {
379+
describe("and primary data already has a #select", () => {
380+
beforeEach(() => {
381+
fetchMock.reset()
382+
fetchMock.get(
383+
"http://example.com/api/v1/people?fields[people]=first_name&fields[books]=title,foo",
384+
{
385+
data: [{ id: "1", type: "people" }]
386+
}
387+
)
388+
})
389+
390+
it("queries correctly", async () => {
391+
const books = Book.select(['title', 'foo'])
392+
const personScope = Person.select(['first_name']).merge({ books })
393+
const { data } = await personScope.all()
394+
395+
expect(data.length).to.eq(1)
396+
expect(data[0]).to.be.instanceof(Person)
397+
})
398+
})
399+
400+
describe("and primary data does not already have a #select", () => {
401+
beforeEach(() => {
402+
fetchMock.reset()
403+
fetchMock.get(
404+
"http://example.com/api/v1/people?fields[books]=title,foo",
405+
{
406+
data: [{ id: "1", type: "people" }]
407+
}
408+
)
409+
})
410+
411+
it("queries correctly", async () => {
412+
const books = Book.select(['title', 'foo'])
413+
const personScope = Person.merge({ books })
414+
const { data } = await personScope.all()
415+
416+
expect(data.length).to.eq(1)
417+
expect(data[0]).to.be.instanceof(Person)
418+
})
419+
})
420+
})
377421
})
378422

379423
describe("#select_extra", () => {
@@ -395,6 +439,50 @@ describe("Model finders", () => {
395439
expect(data[0]).to.be.instanceof(Person)
396440
expect(data[0]).to.have.property("id", "2")
397441
})
442+
443+
describe("when merging association #selectExtra", () => {
444+
describe("and primary data already has a #selectExtra", () => {
445+
beforeEach(() => {
446+
fetchMock.reset()
447+
fetchMock.get(
448+
"http://example.com/api/v1/people?extra_fields[people]=first_name&extra_fields[books]=title,foo",
449+
{
450+
data: [{ id: "1", type: "people" }]
451+
}
452+
)
453+
})
454+
455+
it("queries correctly", async () => {
456+
const books = Book.selectExtra(['title', 'foo'])
457+
const personScope = Person.selectExtra(['first_name']).merge({ books })
458+
const { data } = await personScope.all()
459+
460+
expect(data.length).to.eq(1)
461+
expect(data[0]).to.be.instanceof(Person)
462+
})
463+
})
464+
465+
describe("and primary data does not already have a #selectExtra", () => {
466+
beforeEach(() => {
467+
fetchMock.reset()
468+
fetchMock.get(
469+
"http://example.com/api/v1/people?extra_fields[books]=title,foo",
470+
{
471+
data: [{ id: "1", type: "people" }]
472+
}
473+
)
474+
})
475+
476+
it("queries correctly", async () => {
477+
const books = Book.selectExtra(['title', 'foo'])
478+
const personScope = Person.merge({ books })
479+
const { data } = await personScope.all()
480+
481+
expect(data.length).to.eq(1)
482+
expect(data[0]).to.be.instanceof(Person)
483+
})
484+
})
485+
})
398486
})
399487

400488
describe("#includes", () => {

0 commit comments

Comments
 (0)