
A form updates a saved draft row by position and retains the old state for an undo action. Negative indexes count from the end. The original array survives the replacement.
A runnable JavaScript fixture
The example keeps the input to Array.with outside the function, so it is possible to run the same transformation in a test. The comparison uses concrete data rather than the rendered component. If the application accepts a value from JSON, run the fixture after parsing that JSON to preserve the relevant types.
function evaluate(value) {
return [value[0].with(value[1],value[2]),value[0]];
}
const samples = [[["a", "b", "c"], 1, "B"], [["a", "b"], -1, "Z"], [["x"], 0, "Y"]];
for (const sample of samples) console.log(evaluate(sample));The result for three concrete cases
| Input | Return value |
|---|---|
| [["a", "b", "c"], 1, "B"] | [["a", "B", "c"], ["a", "b", "c"]] |
| [["a", "b"], -1, "Z"] | [["a", "Z"], ["a", "b"]] |
| [["x"], 0, "Y"] | [["Y"], ["x"]] |
For [["a", "b", "c"], 1, "B"], the observed output is [["a", "B", "c"], ["a", "b", "c"]]. With [["a", "b"], -1, "Z"], it becomes [["a", "Z"], ["a", "b"]]. These are useful regression fixtures because an integration test can send both through the same entry point. A test of only the first case can stay green after a change that affects the other.
For [["x"], 0, "Y"], the operation returns 2 top-level items. An assertion on that count would miss a change in their contents. Capture the expected sequence or compare membership explicitly if order has no meaning in the product.
An acceptance check for the real feature
An out-of-range index throws. Validate an index produced by a filtered UI against the same list being updated.
Run one case twice through the real feature and look at both the displayed value and its stored representation. A component may coerce a result to text even when the implementation returns an ordered collection of values. Keep a failing sample small enough that it can travel with a bug report, including the browser or Node version that produced it.
One more fixture before integrating
Store a stable row key alongside any selected position in a filtered editor. A new sort can make index one point at a different record before a delayed update is applied.
The same JavaScript function was also run with [] / [["a", "b", "c"]] / ["B", 1, ["a", "b", "c"]]. These additional inputs change the boundary without changing the implementation. An exception name in the table records where the operation rejects an input; a returned empty or zero value records a successful call. Choose deliberately which of those outcomes the surrounding feature should expose.
| Additional fixture | Result or exception |
|---|---|
| [] | throws TypeError |
| [["a", "b", "c"]] | [[null, "b", "c"], ["a", "b", "c"]] |
| ["B", 1, ["a", "b", "c"]] | throws TypeError |
JavaScript standard library reference · MDN JavaScript language guide
Sources and further reading
Cover photograph: Raspberry Pi Zero 2 W -- 2024 -- 0008 by Anil Öztas. CC BY 4.0. Cropped and typeset for this cover. The photograph illustrates the topic.
