@@ -31,6 +31,19 @@ module('Integration | Component | assert must preload', function(hooks) {
3131 // the next line doesn't work in 2.x due to an eslint rule
3232 // eslint-disable-next-line
3333 [ this . major , this . minor ] = Ember . VERSION . split ( "." ) ;
34+
35+ // setup a bunch of data that our tests will load
36+ let author = this . server . create ( 'author' ) ;
37+ let post = this . server . create ( 'post' , {
38+ id : 1 ,
39+ title : 'Post title' ,
40+ author
41+ } ) ;
42+ let comments = this . server . createList ( 'comment' , 3 , { post } ) ;
43+
44+ comments . forEach ( comment => {
45+ server . create ( 'author' , { comments : [ comment ] } ) ;
46+ } ) ;
3447 } ) ;
3548
3649 hooks . afterEach ( function ( ) {
@@ -41,7 +54,6 @@ module('Integration | Component | assert must preload', function(hooks) {
4154 } ) ;
4255
4356 test ( 'it errors if the relationship has not yet be loaded' , async function ( assert ) {
44- this . server . create ( 'post' ) ;
4557 this . post = await run ( ( ) => {
4658 return this . store . loadRecord ( 'post' , 1 ) ;
4759 } ) ;
@@ -64,7 +76,6 @@ module('Integration | Component | assert must preload', function(hooks) {
6476 } ) ;
6577
6678 test ( 'it errors if one of the relationships has not yet be loaded' , async function ( assert ) {
67- this . server . create ( 'post' ) ;
6879 this . post = await run ( ( ) => {
6980 return this . store . loadRecord ( 'post' , 1 , { include : 'author' } ) ;
7081 } ) ;
@@ -87,7 +98,6 @@ module('Integration | Component | assert must preload', function(hooks) {
8798 } ) ;
8899
89100 test ( 'it errors if a nested relationship has not yet be loaded' , async function ( assert ) {
90- this . server . create ( 'post' ) ;
91101 this . post = await run ( ( ) => {
92102 return this . store . loadRecord ( 'post' , 1 , { include : 'comments' } ) ;
93103 } ) ;
@@ -110,7 +120,6 @@ module('Integration | Component | assert must preload', function(hooks) {
110120 } ) ;
111121
112122 test ( 'it does not error if the relationship was loaded' , async function ( assert ) {
113- this . server . create ( 'post' ) ;
114123 this . post = await run ( ( ) => {
115124 return this . store . loadRecord ( 'post' , 1 , { include : 'comments' } ) ;
116125 } ) ;
@@ -119,8 +128,52 @@ module('Integration | Component | assert must preload', function(hooks) {
119128 {{assert-must-preload post "comments"}}
120129 ` ) ;
121130
122- // if nothing renders, we're ok
131+ // if anything renders, we're ok
123132 assert . dom ( '*' ) . hasText ( '' ) ;
124133 } ) ;
125134
135+ module ( 'Data loaded with loadRecords' , function ( ) {
136+ test ( 'it should not error when all data is loaded' , async function ( assert ) {
137+ let posts = await run ( ( ) => {
138+ return this . store . loadRecords ( 'post' , { include : 'comments' } ) ;
139+ } ) ;
140+
141+ this . post = posts . get ( 'firstObject' ) ;
142+
143+ await render ( hbs `
144+ {{assert-must-preload post "comments"}}
145+
146+ <div data-test-id="title">
147+ {{post.title}}
148+ </div>
149+ ` ) ;
150+
151+ assert . dom ( '[data-test-id="title"]' ) . hasText ( "Post title" ) ;
152+ } ) ;
153+
154+ test ( 'it should error is not all data is loaded' , async function ( assert ) {
155+ let posts = await run ( ( ) => {
156+ return this . store . loadRecords ( 'post' , { include : 'comments,author' } ) ;
157+ } ) ;
158+
159+ this . post = posts . get ( 'firstObject' ) ;
160+
161+ let assertError = function ( e ) {
162+ let regexp = / Y o u t r i e d t o r e n d e r a .+ t h a t a c c e s s e s r e l a t i o n s h i p s o f f o f a p o s t , b u t t h a t m o d e l d i d n ' t h a v e a l l o f i t s r e q u i r e d r e l a t i o n s h i p s p r e l o a d e d ( ' c o m m e n t s .a u t h o r ' ) * / ;
163+ assert . ok ( e . message . match ( regexp ) ) ;
164+ } ;
165+
166+ if ( this . major === "2" && ( this . minor === "12" || this . minor === "16" ) ) {
167+ Ember . Logger . error = function ( ) { } ;
168+ Ember . Test . adapter . exception = assertError ;
169+ } else {
170+ Ember . onerror = assertError ;
171+ }
172+
173+ await render ( hbs `
174+ {{assert-must-preload post "author,comments.author"}}
175+ ` ) ;
176+ } ) ;
177+ } ) ;
178+
126179} ) ;
0 commit comments