Skip to content

Commit

Permalink
fix: merging a field with empty array update doesn't overwrite. (#5)
Browse files Browse the repository at this point in the history
Adds a failing test for this case and introduces a fix.
  • Loading branch information
jadengis authored Jun 17, 2022
1 parent b45eaea commit f63bb49
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 0.10.3
### Fixes

- Fixes `clone` in the case where an empty array is being merged with a non-empty array. Before the
arrays were merged. Now the empty array replacing the existing array.

# 0.10.2

### Fixes
Expand Down
2 changes: 1 addition & 1 deletion packages/model/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@snapdm/model",
"version": "0.10.2",
"version": "0.10.3",
"description": "An opinionated snapshot oriented modeling system for Cloud Firestore",
"publishConfig": {
"access": "public"
Expand Down
53 changes: 52 additions & 1 deletion packages/model/src/lib/model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type FooInitializer = Readonly<{
value: string;
}>;

interface Foo extends FooData {}
interface Foo extends FooData { }

class Foo extends Model<FooData, FooInitializer>({
type: 'Foo',
Expand Down Expand Up @@ -68,6 +68,33 @@ class Baz extends Model<BazData, BazInitializer>({
}
}

type WithArrayData = Readonly<{
array: string[];
}>;

interface WithArray extends WithArrayData { }

class WithArray extends Model<WithArrayData, WithArrayData>({
type: 'WithArray',
collection: 'withArray',
}) { }

type File = Readonly<{
type: string;
name: string;
}>;

type FolderData = Readonly<{
files: File[];
}>;

interface Folder extends FolderData { }

class Folder extends Model<FolderData, FolderData>({
type: 'Folder',
collection: 'folders',
}) { }

describe('Model', () => {
describe('static methods', () => {
const subject = () => Foo;
Expand Down Expand Up @@ -205,4 +232,28 @@ describe('Model', () => {
});
});
});

describe('with arrays', () => {
describe('simple array', () => {
const withArray = new WithArray({ array: ['foo'] });

[[], ['bar']].forEach((test) => {
it('should replace the array', () => {
const result = withArray.clone({ array: test });
expect(result.array).toEqual(test);
});
});
});

describe('complex array', () => {
const folder = new Folder({ files: [{ type: 'File', name: 'wowza' }] });

[[], [{ type: 'File', name: 'blue blue' }]].forEach((test) => {
it('should replace the array', () => {
const result = folder.clone({ files: test });
expect(result.files).toEqual(test);
});
});
});
});
});
6 changes: 4 additions & 2 deletions packages/model/src/lib/utils/merge.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import __merge from 'lodash/merge';
import __mergeWith from 'lodash/mergeWith';

export function merge<T1, T2>(t1: T1, t2: T2): T1 & T2;
export function merge<T1, T2, T3>(t1: T1, t2: T2, t3: T3): T1 & T2 & T3;
Expand All @@ -9,5 +9,7 @@ export function merge<T1, T2, T3, T4>(
t4: T4
): T1 & T2 & T3 & T4;
export function merge(...sources: unknown[]): unknown {
return __merge({}, ...sources);
return __mergeWith({}, ...sources, (obj: unknown, val: unknown) =>
Array.isArray(obj) ? val : undefined
);
}
2 changes: 1 addition & 1 deletion packages/preconditions/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@snapdm/preconditions",
"version": "0.10.2",
"version": "0.10.3",
"description": "General purpose preconditions for typesafe Typescript libraries",
"publishConfig": {
"access": "public"
Expand Down

0 comments on commit f63bb49

Please sign in to comment.