From af02989ea33b61f1ed8ba5db7893fbb6cf73dd78 Mon Sep 17 00:00:00 2001 From: Chris Santamaria Date: Fri, 25 Jun 2021 18:13:02 -0400 Subject: [PATCH 1/3] Add failing test --- __tests__/base.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/__tests__/base.js b/__tests__/base.js index 29721c1c..22a75472 100644 --- a/__tests__/base.js +++ b/__tests__/base.js @@ -883,6 +883,24 @@ function runBaseTest(name, useProxies, autoFreeze, useListener) { expect(d instanceof Set).toBeTruthy() }) }) + + it("maintains order when adding", () => { + const objs = [ + { + id: "a" + }, + { + id: "b" + } + ] + + const set = new Set([objs[0]]) + const newSet = produce(set, draft => { + draft.add(objs[1]) + }) + + expect(Array.from(newSet)).toEqual([objs[0], objs[1]]) + }) }) it("supports `immerable` symbol on constructor", () => { From e26c551d15123f715ccdd3435fdf7466a7934b90 Mon Sep 17 00:00:00 2001 From: Chris Santamaria Date: Mon, 28 Jun 2021 15:21:57 -0400 Subject: [PATCH 2/3] Move test to map-set --- __tests__/base.js | 18 ------------------ __tests__/map-set.js | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/__tests__/base.js b/__tests__/base.js index 22a75472..29721c1c 100644 --- a/__tests__/base.js +++ b/__tests__/base.js @@ -883,24 +883,6 @@ function runBaseTest(name, useProxies, autoFreeze, useListener) { expect(d instanceof Set).toBeTruthy() }) }) - - it("maintains order when adding", () => { - const objs = [ - { - id: "a" - }, - { - id: "b" - } - ] - - const set = new Set([objs[0]]) - const newSet = produce(set, draft => { - draft.add(objs[1]) - }) - - expect(Array.from(newSet)).toEqual([objs[0], objs[1]]) - }) }) it("supports `immerable` symbol on constructor", () => { diff --git a/__tests__/map-set.js b/__tests__/map-set.js index 8bf726f9..f8c1ba9c 100644 --- a/__tests__/map-set.js +++ b/__tests__/map-set.js @@ -295,5 +295,23 @@ function runBaseTest(name, useProxies, autoFreeze, useListener) { }) expect(mapType1).toBe(mapType2) }) + + test("#819 - Set maintains order when adding", () => { + const objs = [ + { + id: "a" + }, + { + id: "b" + } + ] + + const set = new Set([objs[0]]) + const newSet = produce(set, draft => { + draft.add(objs[1]) + }) + + expect(Array.from(newSet)).toEqual([objs[0], objs[1]]) + }) }) } From 38fa33c7b4892e3e706c8cde703dde82ed6ca13e Mon Sep 17 00:00:00 2001 From: Chris Santamaria Date: Mon, 28 Jun 2021 15:25:33 -0400 Subject: [PATCH 3/3] Add string variant of first test --- __tests__/map-set.js | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/__tests__/map-set.js b/__tests__/map-set.js index f8c1ba9c..2108c0b6 100644 --- a/__tests__/map-set.js +++ b/__tests__/map-set.js @@ -296,8 +296,8 @@ function runBaseTest(name, useProxies, autoFreeze, useListener) { expect(mapType1).toBe(mapType2) }) - test("#819 - Set maintains order when adding", () => { - const objs = [ + test("#819 - Set with object maintains order when adding object", () => { + const items = [ { id: "a" }, @@ -306,12 +306,29 @@ function runBaseTest(name, useProxies, autoFreeze, useListener) { } ] - const set = new Set([objs[0]]) + const set = new Set([items[0]]) const newSet = produce(set, draft => { - draft.add(objs[1]) + draft.add(items[1]) }) - expect(Array.from(newSet)).toEqual([objs[0], objs[1]]) + expect(Array.from(newSet)).toEqual([items[0], items[1]]) + }) + + // More specific varaint of above test covering case of adding non-object item + test("#819 - Set with object maintains order when adding string", () => { + const items = [ + { + id: "a" + }, + "b" + ] + + const set = new Set([items[0]]) + const newSet = produce(set, draft => { + draft.add(items[1]) + }) + + expect(Array.from(newSet)).toEqual([items[0], items[1]]) }) }) }