-
-
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.
Failing identifier polymorphic scenario
- Loading branch information
Showing
1 changed file
with
115 additions
and
0 deletions.
There are no files selected for viewing
115 changes: 115 additions & 0 deletions
115
packages/-ember-data/tests/integration/identifiers/polymorphic-scenarios-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,115 @@ | ||
import { module, test } from 'qunit'; | ||
import { setupTest } from 'ember-qunit'; | ||
import { IDENTIFIERS } from '@ember-data/canary-features'; | ||
import Store from '@ember-data/store'; | ||
import Model, { attr, belongsTo } from '@ember-data/model'; | ||
import Adapter from '@ember-data/adapter'; | ||
import Serializer from '@ember-data/serializer'; | ||
import { resolve } from 'rsvp'; | ||
|
||
if (IDENTIFIERS) { | ||
module('Integration | Identifiers - polymorphic scenarios', function(hooks) { | ||
setupTest(hooks); | ||
|
||
module('single-table', function(hooks) { | ||
let store; | ||
let calls; | ||
class TestSerializer extends Serializer { | ||
normalizeResponse(_, __, payload) { | ||
return payload; | ||
} | ||
} | ||
class TestAdapter extends Adapter { | ||
shouldBackgroundReloadRecord() { | ||
return false; | ||
} | ||
} | ||
|
||
class CarAdapter extends TestAdapter { | ||
findRecord() { | ||
return resolve({ | ||
data: { | ||
id: '1', | ||
type: 'ferrari', | ||
attributes: { | ||
color: 'red' | ||
}, | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
class FerrariAdapter extends TestAdapter { | ||
findRecord() { | ||
return resolve({ | ||
data: { | ||
id: '1', | ||
type: 'ferrari', | ||
attributes: { | ||
color: 'red' | ||
}, | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
class DealershipAdapter extends TestAdapter { | ||
findRecord() { | ||
return resolve({ | ||
data: { | ||
id: '1', | ||
type: 'dealership', | ||
attributes: { | ||
name: 'Brand new* car' | ||
}, | ||
relationships: { | ||
car: { | ||
data: { id: 1, type: 'ferrari' }, | ||
}, | ||
}, | ||
} | ||
}); | ||
} | ||
} | ||
|
||
hooks.beforeEach(function() { | ||
const { owner } = this; | ||
|
||
class Car extends Model { | ||
@attr() color: string; | ||
} | ||
|
||
class Ferrari extends Car { | ||
@attr() color: string; | ||
} | ||
|
||
class Dealership extends Model { | ||
@attr() name: string; | ||
@belongsTo("car", { polymorphic: true }) car; | ||
} | ||
|
||
owner.register('adapter:car', CarAdapter); | ||
owner.register('adapter:ferrari', FerrariAdapter); | ||
owner.register('adapter:dealership', DealershipAdapter); | ||
owner.register('serializer:application', TestSerializer); | ||
owner.register('model:car', Car); | ||
owner.register('model:ferrari', Ferrari); | ||
owner.register('model:dealership', Dealership); | ||
owner.register('service:store', Store); | ||
|
||
store = owner.lookup('service:store'); | ||
|
||
}); | ||
|
||
test(`Identity of polymorphic relations can change type`, async function(assert) { | ||
const topRecord = await store.findRecord('dealership', '1'); | ||
const relation = await topRecord.get('car'); | ||
|
||
assert.strictEqual(relation.id, '1'); | ||
|
||
const foundRecord = await store.findRecord('car', '1'); | ||
assert.strictEqual(foundRecord.id, '1'); | ||
}); | ||
}); | ||
}); | ||
} |