adopted-ember-addons / ember-data-model-fragments

Ember Data addon to support nested JSON documents
MIT License
370 stars 114 forks source link

Fix updating fragment arrays multiple times in the same runloop #487

Closed dwickern closed 4 months ago

dwickern commented 4 months ago

In production builds, certain fragment array mutations produce the wrong result. For example, this code swaps the first two items in the array:

const first = fragmentArray.objectAt(0)
fragmentArray.removeAt(0)
fragmentArray.insertAt(1, first)

In development, this works as expected. But in a production build, this code results in the first item being duplicated (as if the removeAt did not happen).

There are already some failing tests for this issue, as long as we run the tests in production mode:

not ok 19 Chrome 124.0 - [23 ms] - integration - Dependent State: reordering a fragment array dirties the fragment array and owner record
    ---
        actual: >
            3
        expected: >
            2
        stack: >
                at Object.<anonymous> (http://localhost:7357/assets/tests-28c38f66d4715374e954e49f4d4e8a30.js:39:22)
        message: >
            fragment array length is maintained
        negative: >
            false
        browser log: |
    ...

not ok 110 Chrome 124.0 - [23 ms] - unit - `MF.fragmentArray`: changes to array contents change the fragment array `hasDirtyAttributes` property
    ---
        actual: >
            false
        expected: >
            true
        stack: >
                at Object.<anonymous> (http://localhost:7357/assets/tests-28c38f66d4715374e954e49f4d4e8a30.js:306:193)
        message: >
            fragment array is returned to clean state
        negative: >
            false
        browser log: |
    ...

Some of the existing tests depend on Ember.assert throwing an exception. To make this work in production mode, I've replaced assert.throws with expectAssertion, which is similar to ember-qunit-assert-helpers and to ember's own expectAssertion helper.

knownasilya commented 4 months ago

Released as v6.0.9