-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
packages/unpublished-test-infra/addon-test-support/test-in-production.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { type TestContext } from '@ember/test-helpers'; | ||
|
||
import { skip, test as qunitTest } from 'qunit'; | ||
|
||
import { DEBUG } from '@ember-data/env'; | ||
|
||
export function productionTest( | ||
label: string, | ||
callback: (this: TestContext, assert: Assert) => void | Promise<void> | ||
): void { | ||
if (DEBUG) { | ||
skip(`[PROD-ONLY] ${label}`, callback); | ||
} else { | ||
qunitTest(`[PROD-ONLY] ${label}`, callback); | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
tests/main/tests/integration/relationships/unloaded-data-test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { module } from 'qunit'; | ||
|
||
import { setupTest } from 'ember-qunit'; | ||
|
||
import { graphFor } from '@ember-data/graph/-private'; | ||
import Model, { attr, belongsTo, hasMany } from '@ember-data/model'; | ||
import type Store from '@ember-data/store'; | ||
import { CollectionRelationship } from '@ember-data/types/cache/relationship'; | ||
import { productionTest } from '@ember-data/unpublished-test-infra/test-support/test-in-production'; | ||
|
||
module('integration/relationships/unloaded-data', function (hooks) { | ||
setupTest(hooks); | ||
|
||
productionTest( | ||
'unloaded records in a hasMany alert the RecordArray when they load (inverse null)', | ||
function (assert) { | ||
class App extends Model { | ||
@attr declare name: string; | ||
@hasMany('config', { async: false, inverse: null }) declare configs: Config[]; | ||
} | ||
class Config extends Model { | ||
@attr declare name: string; | ||
@belongsTo('app', { async: false, inverse: 'configs' }) declare app: App | null; | ||
} | ||
|
||
this.owner.register('model:app', App); | ||
this.owner.register('model:config', Config); | ||
|
||
const store = this.owner.lookup('service:store') as Store; | ||
const graph = graphFor(store); | ||
const identifier = store.identifierCache.getOrCreateRecordIdentifier({ | ||
type: 'app', | ||
id: '1', | ||
}); | ||
|
||
const app = store.push({ | ||
data: { | ||
id: '1', | ||
type: 'app', | ||
attributes: { | ||
name: 'My App', | ||
}, | ||
relationships: { | ||
configs: { | ||
data: [ | ||
{ | ||
id: '1', | ||
type: 'config', | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
}) as App; | ||
|
||
// "pull" on the ManyArray to materialize it else the laziness will not manifest | ||
// in the bug we are testing (see emberjs/data#8846 for context) | ||
assert.strictEqual(app.configs.length, 0, 'no configs yet because the record is unloaded'); | ||
assert.strictEqual( | ||
(graph.getData(identifier, 'configs') as CollectionRelationship).data?.length, | ||
1, | ||
'the record is in the graph' | ||
); | ||
|
||
// now load the related record | ||
store.push({ | ||
data: { | ||
id: '1', | ||
type: 'config', | ||
attributes: { | ||
name: 'My Config', | ||
}, | ||
}, | ||
}); | ||
|
||
// now check that we've gotten into the correct state | ||
assert.strictEqual(app.configs.length, 1, 'the record is now loaded'); | ||
assert.strictEqual( | ||
(graph.getData(identifier, 'configs') as CollectionRelationship).data?.length, | ||
1, | ||
'the record is in the graph' | ||
); | ||
} | ||
); | ||
}); |