Skip to content

Commit

Permalink
refactor: minor
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Oct 31, 2024
1 parent 83741e0 commit 80accd6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/snapshot/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export interface SnapshotClientOptions {
isEqual?: (received: unknown, expected: unknown) => boolean
}

// TODO: probably we don't need much refactoring here yet. separate refactoring
export class SnapshotClient {
snapshotStateMap: Map<string, SnapshotState> = new Map()

Expand Down
9 changes: 5 additions & 4 deletions packages/snapshot/src/port/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { saveRawSnapshots } from './rawSnapshot'

import {
addExtraLineBreaks,
DefaultMap,
getSnapshotData,
keyToTestName,
normalizeNewlines,
Expand Down Expand Up @@ -54,7 +55,7 @@ export default class SnapshotState {
private _initialData: SnapshotData
private _inlineSnapshots: Array<InlineSnapshot>
private _inlineSnapshotStacks: Array<ParsedStack & { testId: string }>
private _testIdToKeys = new Map<string, string[]>()
private _testIdToKeys = new DefaultMap<string, string[]>(() => [])
private _rawSnapshots: Array<RawSnapshot>
private _uncheckedKeys: Set<string>
private _snapshotFormat: PrettyFormatOptions
Expand Down Expand Up @@ -125,7 +126,7 @@ export default class SnapshotState {
this._inlineSnapshots = this._inlineSnapshots.filter(s => s.testId !== testId)
this._inlineSnapshotStacks = this._inlineSnapshotStacks.filter(s => s.testId !== testId)

for (const key of this._testIdToKeys.get(testId) ?? []) {
for (const key of this._testIdToKeys.get(testId)) {
const name = keyToTestName(key)
const counter = this._counters.get(name)
if (typeof counter !== 'undefined') {
Expand Down Expand Up @@ -252,14 +253,14 @@ export default class SnapshotState {
error,
rawSnapshot,
}: SnapshotMatchOptions): SnapshotReturnOptions {
// this also increments counter for inline snapshots. maybe we don't need to?
this._counters.set(testName, (this._counters.get(testName) || 0) + 1)
const count = Number(this._counters.get(testName))

if (!key) {
key = testNameToKey(testName, count)
}
this._testIdToKeys.set(testId, (this._testIdToKeys.get(testId) ?? []))
this._testIdToKeys.get(testId)?.push(key)
this._testIdToKeys.get(testId).push(key)

// Do not mark the snapshot as "checked" if the snapshot is inline and
// there's an external snapshot. This way the external snapshot can be
Expand Down
16 changes: 16 additions & 0 deletions packages/snapshot/src/port/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,19 @@ export function deepMergeSnapshot(target: any, source: any): any {
}
return target
}

export class DefaultMap<K, V> extends Map<K, V> {
constructor(
private defaultFn: (key: K) => V,
entries?: Iterable<readonly [K, V]>,
) {
super(entries)
}

override get(key: K): V {
if (!this.has(key)) {
this.set(key, this.defaultFn(key))
}
return super.get(key)!
}
}

0 comments on commit 80accd6

Please sign in to comment.