Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added more cases for testing native access
- Loading branch information
Larry Yu
committed
Feb 16, 2023
1 parent
cdffecc
commit ff0d36a
Showing
2 changed files
with
141 additions
and
70 deletions.
There are no files selected for viewing
70 changes: 0 additions & 70 deletions
70
tests/unit/model/native-access/native-access-arrays-first-object-test.js
This file was deleted.
Oops, something went wrong.
141 changes: 141 additions & 0 deletions
141
tests/unit/model/native-access/native-access-surrogate-method-test.js
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,141 @@ | ||
/** | ||
* A test case to show that 'firstObject' doesn't return | ||
* the current value of a ManagedArray with ember-source | ||
* v4.10.0 due to its change of property notification. | ||
*/ | ||
import { get } from '@ember/object'; | ||
import { module, test } from 'qunit'; | ||
import { setupTest } from 'ember-qunit'; | ||
import DefaultSchema from 'ember-m3/services/m3-schema'; | ||
|
||
function computeNestedModel(key, value /*, modelName, schemaInterface */) { | ||
if (typeof value === 'object' && value !== null && !Array.isArray(value)) { | ||
return { | ||
attributes: value, | ||
}; | ||
} | ||
} | ||
|
||
class TestSchema extends DefaultSchema { | ||
includesModel(modelName) { | ||
return /^com.example.bookstore\./i.test(modelName); | ||
} | ||
computeAttribute(key, value, modelName, schemaInterface) { | ||
if (Array.isArray(value)) { | ||
let nested = value.map((v) => { | ||
if (typeof v === 'object') { | ||
return schemaInterface.nested(computeNestedModel(key, v, modelName, schemaInterface)); | ||
} else { | ||
return v; | ||
} | ||
}); | ||
return schemaInterface.managedArray(nested); | ||
} else { | ||
let nested = computeNestedModel(key, value, modelName, schemaInterface); | ||
if (nested) { | ||
return schemaInterface.nested(nested); | ||
} | ||
} | ||
} | ||
} | ||
|
||
module(`unit/model/native-access/native-access-arrays/first-object`, function (hooks) { | ||
setupTest(hooks); | ||
|
||
hooks.beforeEach(function () { | ||
this.owner.register('service:m3-schema', TestSchema); | ||
this.store = this.owner.lookup('service:store'); | ||
}); | ||
|
||
test('Empty Array: firstObject should work', function (assert) { | ||
let model = this.store.push({ | ||
data: { | ||
id: 'isbn:9780439708180', | ||
type: 'com.example.bookstore.Book', | ||
attributes: { | ||
name: `Harry Potter and the Sorcerer's Stone`, | ||
chapters: [], | ||
}, | ||
}, | ||
}); | ||
|
||
let chapters = model.get('chapters'); | ||
get(chapters, 'firstObject.name'); // this READ would screw up the internal versioning state | ||
chapters.pushObject({ name: 'The Boy Who Lived' }); | ||
|
||
assert.equal(get(chapters[0], 'name'), 'The Boy Who Lived', `[] reference works`); | ||
// this assertion would fail with ember-source@4.10.0 | ||
assert.equal(get(chapters, 'firstObject.name'), 'The Boy Who Lived', `firstObject works`); | ||
}); | ||
|
||
test('Empty Array: lastObject should work', function (assert) { | ||
let model = this.store.push({ | ||
data: { | ||
id: 'isbn:9780439708180', | ||
type: 'com.example.bookstore.Book', | ||
attributes: { | ||
name: `Harry Potter and the Sorcerer's Stone`, | ||
chapters: [], | ||
}, | ||
}, | ||
}); | ||
|
||
let chapters = model.get('chapters'); | ||
get(chapters, 'lastObject.name'); // this READ would screw up the internal versioning state | ||
chapters.pushObject({ name: 'The Boy Who Lived' }); | ||
|
||
assert.equal( | ||
get(chapters[chapters.length - 1], 'name'), | ||
'The Boy Who Lived', | ||
`[] reference works` | ||
); | ||
// this assertion would fail with ember-source@4.10.0 | ||
assert.equal(get(chapters, 'lastObject.name'), 'The Boy Who Lived', `lastObject works`); | ||
}); | ||
|
||
test('Non-empty Array: firstObject should work', function (assert) { | ||
let model = this.store.push({ | ||
data: { | ||
id: 'isbn:9780439708180', | ||
type: 'com.example.bookstore.Book', | ||
attributes: { | ||
name: `Harry Potter and the Sorcerer's Stone`, | ||
chapters: [{ name: 'The Boy Who Lived' }], | ||
}, | ||
}, | ||
}); | ||
|
||
let chapters = model.get('chapters'); | ||
get(chapters, 'firstObject.name'); // this READ would screw up the internal versioning state | ||
chapters.pushObject({ name: 'Lives Forever!' }); | ||
|
||
assert.equal(get(chapters[0], 'name'), 'The Boy Who Lived', `[] reference works`); | ||
// this assertion would fail with ember-source@4.10.0 | ||
assert.equal(get(chapters, 'firstObject.name'), 'The Boy Who Lived', `firstObject works`); | ||
}); | ||
|
||
test('Non-empty Array: lastObject should work', function (assert) { | ||
let model = this.store.push({ | ||
data: { | ||
id: 'isbn:9780439708180', | ||
type: 'com.example.bookstore.Book', | ||
attributes: { | ||
name: `Harry Potter and the Sorcerer's Stone`, | ||
chapters: [{ name: 'The Boy Who Lived' }], | ||
}, | ||
}, | ||
}); | ||
|
||
let chapters = model.get('chapters'); | ||
get(chapters, 'lastObject.name'); // this READ would screw up the internal versioning state | ||
chapters.pushObject({ name: 'Lives Forever!' }); | ||
|
||
assert.equal( | ||
get(chapters[chapters.length - 1], 'name'), | ||
'Lives Forever!', | ||
`[] reference works` | ||
); | ||
// this assertion would fail with ember-source@4.10.0 | ||
assert.equal(get(chapters, 'lastObject.name'), 'Lives Forever!', `lastObject works`); | ||
}); | ||
}); |