Skip to content

Commit

Permalink
Add test cases for nested relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcos Javier Alvarez Maestre committed Jul 4, 2019
1 parent f5163ea commit def32d7
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 4 deletions.
44 changes: 42 additions & 2 deletions __testHelpers__/fixtures/specDetailComsNormalized.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,38 @@ export default {
specs: [11],
areas: [51],
specDetailComs: [99],
specCategories: [7]
specCategories: [7],
areaRooms: [13, 14, 15],
areaTypes: [2, 3],
},
areaTypes: {
2: {
id: 2,
type: 'areaTypes',
attributes: { areaTypeData: "Should show up" }
},
3: {
id: 3,
type: 'areaTypes',
attributes: { areaTypeData: "Should NOT show up" }
}
},
areaRooms: {
13: {
id: 13,
type: 'areaRooms',
attributes: { areaRoomData: "Should show up" }
},
14: {
id: 14,
type: 'areaRooms',
attributes: { areaRoomData: "Should show up" }
},
15: {
id: 15,
type: 'areaRooms',
attributes: { areaRoomData: "Should NOT show up" }
}
},
specs: {
10: {
Expand Down Expand Up @@ -118,7 +149,16 @@ export default {
51: {
id: 51,
type: "areas",
attributes: {areasData: "Should show up"}
attributes: {areasData: "Should show up"},
relationships: {
areaType: {data: {id: 2, type: "areaTypes"}},
areaRooms: {
data: [
{id: 13, type: "areaRooms"},
{id: 14, type: "areaRooms"},
]
},
}
}
}
};
7 changes: 6 additions & 1 deletion __testHelpers__/models/Area.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import BaseModel from "../../src/BaseModel";
import Spec from "./Spec";
import AreaType from "./AreaType";
import AreaRoom from "./AreaRoom";

export default class Area extends BaseModel {
static get belongsTo() {
return [Spec];
return [Spec, AreaType];
}
static get hasMany(){
return [AreaRoom];
}
}
8 changes: 8 additions & 0 deletions __testHelpers__/models/AreaRoom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import BaseModel from "../../src/BaseModel";
import Area from "./Area";

export default class AreaRoom extends BaseModel {
static get belongsTo(){
return [Area]
}
}
4 changes: 4 additions & 0 deletions __testHelpers__/models/AreaType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import BaseModel from "../../src/BaseModel";
import Spec from "./Spec";

export default class AreaType extends BaseModel {}
34 changes: 33 additions & 1 deletion __tests__/NestedRelationships.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
Checklist,
PurchaseOrderContact,
Spec,
SpecDetailCom
SpecDetailCom,
} from "../__testHelpers__/models";

describe("Nested resources", () => {
Expand Down Expand Up @@ -97,6 +97,38 @@ describe("Nested resources", () => {
expect(specDetailComs).toMatchSnapshot();
});

test("belongsTo.belongsTo.belongsTo.belongsTo", () => {
const specDetailComs = SpecDetailCom.query(specDetailComsResources)
.where({id: [97]})
.includes(["specDetail.spec.area.areaType"])
.toObjects();
expect(specDetailComs).toMatchSnapshot();
});

test("belongsTo.belongsTo.belongsTo.hasMany", () => {
const specDetailComs = SpecDetailCom.query(specDetailComsResources)
.where({id: [97]})
.includes(["specDetail.spec.area.areaRooms"])
.toObjects();
expect(specDetailComs).toMatchSnapshot();
});

test("belongsTo.belongsTo.[belongsTo.belongsTo, belongsTo]", () => {
const specDetailComs = SpecDetailCom.query(specDetailComsResources)
.where({id: [97]})
.includes(["specDetail.spec.[area.areaType, specCategory]"])
.toObjects();
expect(specDetailComs).toMatchSnapshot();
});

test("belongsTo.belongsTo.[belongsTo.hasMany, belongsTo]", () => {
const specDetailComs = SpecDetailCom.query(specDetailComsResources)
.where({id: [97]})
.includes(["specDetail.spec.[area.areaRooms, specCategory]"])
.toObjects();
expect(specDetailComs).toMatchSnapshot();
});

test("belongsTo.[hasMany, belongsTo], switched order", () => {
const specDetailComs = SpecDetailCom.query(specDetailComsResources)
.where({id: [99]})
Expand Down
76 changes: 76 additions & 0 deletions __tests__/__snapshots__/NestedRelationships.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,82 @@ Array [
]
`;

exports[`Nested resources belongsTo.belongsTo.[belongsTo.belongsTo, belongsTo] 1`] = `
Array [
Object {
"comData": "Should show up",
"id": 97,
"specDetail": Object {
"id": 5,
"spec": Object {
"id": 12,
"specCategory": Object {
"id": 7,
"specCategoryData": "Should show up",
},
"specData": "Should show up",
},
"specDetailsData": "Should show up",
},
},
]
`;

exports[`Nested resources belongsTo.belongsTo.[belongsTo.hasMany, belongsTo] 1`] = `
Array [
Object {
"comData": "Should show up",
"id": 97,
"specDetail": Object {
"id": 5,
"spec": Object {
"id": 12,
"specCategory": Object {
"id": 7,
"specCategoryData": "Should show up",
},
"specData": "Should show up",
},
"specDetailsData": "Should show up",
},
},
]
`;

exports[`Nested resources belongsTo.belongsTo.belongsTo.belongsTo 1`] = `
Array [
Object {
"comData": "Should show up",
"id": 97,
"specDetail": Object {
"id": 5,
"spec": Object {
"id": 12,
"specData": "Should show up",
},
"specDetailsData": "Should show up",
},
},
]
`;

exports[`Nested resources belongsTo.belongsTo.belongsTo.hasMany 1`] = `
Array [
Object {
"comData": "Should show up",
"id": 97,
"specDetail": Object {
"id": 5,
"spec": Object {
"id": 12,
"specData": "Should show up",
},
"specDetailsData": "Should show up",
},
},
]
`;

exports[`Nested resources hasMany.[belongsTo, hasMany, hasMany], belongsTo, switched order 1`] = `
Array [
Object {
Expand Down

0 comments on commit def32d7

Please sign in to comment.