Skip to content

Commit

Permalink
test: omit default values from props snapshots (#680)
Browse files Browse the repository at this point in the history
* test: omit uninteresting mock data from snapshots

* test: improve default StateMock toJSON readability

* test: another StateMock snapshot readability tweak
  • Loading branch information
ckerr authored May 3, 2021
1 parent 66b147c commit 397e9f6
Show file tree
Hide file tree
Showing 5 changed files with 291 additions and 4,440 deletions.
37 changes: 36 additions & 1 deletion tests/mocks/state.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { observable } from 'mobx';
import { observable, toJS } from 'mobx';
import { MosaicNode } from 'react-mosaic-component';

import {
Expand Down Expand Up @@ -123,4 +123,39 @@ export class StateMock {
this.version = version;
this.currentElectronVersion = mockVersions[version];
}

// Invoked when a snapshot is made.
//
// Snapshots get very "noisy" when they include the entire Mock's details.
// We can avoid most of that noise by hiding fields that hold default values.
public toJSON() {
const defaultValues = new StateMock();
const serialize = (input: any) => JSON.stringify(toJS(input));
const isDefaultValue = (key: string, value: any) =>
serialize(value) === serialize(defaultValues[key]);
const terserRunnable = (ver: RunnableVersion) =>
[ver.version, ver.source, ver.state].join(' ');

const o = {};
for (const entry of Object.entries(this)) {
const key = entry[0];
let val = toJS(entry[1]);

// omit any fields that have the default value
if (isDefaultValue(key, val)) continue;

// make some verbose properties a little terser
if (key == 'currentElectronVersion') {
val = terserRunnable(val);
} else if (key === 'versions') {
val = Object.values(val).map(terserRunnable);
} else if (key === 'versionsToShow') {
val = val.map(terserRunnable);
}

o[key] = val;
}

return Object.keys(o).length === 0 ? 'default StateMock' : o;
}
}
Loading

0 comments on commit 397e9f6

Please sign in to comment.