Skip to content

Commit

Permalink
Snapshot emits default when no snapshot is available
Browse files Browse the repository at this point in the history
  • Loading branch information
danielefiungo committed Nov 15, 2020
1 parent 547c537 commit 37405e0
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/__test__/ghii.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,14 @@ describe('Ghii Config', () => {

describe('history and version', () => {
it('have empty history and latestVersion if no snapshot is taken', () => {
const target = Ghii<{ a: { test: 'string' } }>().section('a', {
const target = Ghii<{ a: { test: 'string' | 'defaults' } }>().section('a', {
validator: joi => joi.string(),
defaults: { test: 'defaults' },
});

expect(target.history()).toStrictEqual([]);
expect(target.latestVersion()).toBeUndefined();
expect(target.snapshot()).toBeUndefined();
expect(target.snapshot()).toStrictEqual({ a: { test: 'defaults' } });
});

it('have history and latestVersion if snapshot is taken', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/ghii.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type GhiiInstance<O extends { [P in keyof O]: O[P] }> = {
loader: (this: GhiiInstance<O>, loader: Loader) => GhiiInstance<O>;
takeSnapshot: () => Promise<{ [key in keyof O]: O[key] }>;
history: () => SnapshotVersion<O>[];
snapshot: (newSnapshot?: Snapshot<O>) => O | undefined;
snapshot: (newSnapshot?: Snapshot<O>) => O;
latestVersion: () => SnapshotVersion<O> | undefined;
waitForFirstSnapshot: (moduleToLoad: string) => Promise<void>;
on: ValueOf<Pick<GhiiEmitter<EventTypes<O>>, 'on'>>;
Expand Down Expand Up @@ -122,7 +122,7 @@ export function ghii<O extends { [P in keyof O]: O[P] }>(): GhiiInstance<O> {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
events.emit('ghii:version:new', latestVersion()!);
}
return latestVersion()?.value;
return latestVersion()?.value ?? prepareDefaults(sections);
}

function waitForFirstSnapshot(moduleToLoad: string) {
Expand Down

0 comments on commit 37405e0

Please sign in to comment.