From 727f8a0de101bba0079f560435d33e6533839dbe Mon Sep 17 00:00:00 2001 From: Guillaume Roux Date: Wed, 12 Jun 2024 12:17:46 +0200 Subject: [PATCH] add tests and update coverage --- packages/snaps-simulator/jest.config.js | 8 +-- .../src/contexts/utils.test.ts | 19 +++++++ .../src/features/simulation/slice.test.ts | 49 +++++++++++++++++++ .../snaps-simulator/src/utils/render.test.tsx | 29 +++++++++++ packages/snaps-utils/coverage.json | 8 +-- packages/snaps-utils/src/ui.test.tsx | 29 +++++++++++ 6 files changed, 134 insertions(+), 8 deletions(-) create mode 100644 packages/snaps-simulator/src/contexts/utils.test.ts create mode 100644 packages/snaps-simulator/src/utils/render.test.tsx diff --git a/packages/snaps-simulator/jest.config.js b/packages/snaps-simulator/jest.config.js index e3fddc4590..1588c3a2e7 100644 --- a/packages/snaps-simulator/jest.config.js +++ b/packages/snaps-simulator/jest.config.js @@ -7,10 +7,10 @@ delete baseConfig.transform; module.exports = deepmerge(baseConfig, { coverageThreshold: { global: { - branches: 55.98, - functions: 61.72, - lines: 81.48, - statements: 81.71, + branches: 54.18, + functions: 60.76, + lines: 80.49, + statements: 80.83, }, }, setupFiles: ['./jest.setup.js'], diff --git a/packages/snaps-simulator/src/contexts/utils.test.ts b/packages/snaps-simulator/src/contexts/utils.test.ts new file mode 100644 index 0000000000..18736e9e70 --- /dev/null +++ b/packages/snaps-simulator/src/contexts/utils.test.ts @@ -0,0 +1,19 @@ +import { mergeValue } from './utils'; + +describe('mergeValue', () => { + it('merges a value outside of a form', () => { + const state = { foo: 'bar' }; + + const result = mergeValue(state, 'foo', 'baz'); + + expect(result).toStrictEqual({ foo: 'baz' }); + }); + + it('merges a value inside of a form', () => { + const state = { foo: { bar: 'baz' } }; + + const result = mergeValue(state, 'bar', 'test', 'foo'); + + expect(result).toStrictEqual({ foo: { bar: 'test' } }); + }); +}); diff --git a/packages/snaps-simulator/src/features/simulation/slice.test.ts b/packages/snaps-simulator/src/features/simulation/slice.test.ts index cdb482ae0d..5c7b90e7c3 100644 --- a/packages/snaps-simulator/src/features/simulation/slice.test.ts +++ b/packages/snaps-simulator/src/features/simulation/slice.test.ts @@ -1,6 +1,9 @@ import type { IframeExecutionService } from '@metamask/snaps-controllers'; +import type { SnapId } from '@metamask/snaps-sdk'; +import { Text } from '@metamask/snaps-sdk/jsx'; import { VirtualFile } from '@metamask/snaps-utils'; +import type { SnapInterface } from './slice'; import { SnapStatus, simulation as reducer, @@ -13,7 +16,11 @@ import { setSnapState, setUnencryptedSnapState, setLocalizationFiles, + setSnapInterface, + setSnapInterfaceController, + setSnapInterfaceState, } from './slice'; +import { getSnapInterfaceController } from './test/controllers'; import { MockExecutionService } from './test/mockExecutionService'; import { MOCK_MANIFEST, MOCK_MANIFEST_FILE } from './test/mockManifest'; @@ -39,6 +46,21 @@ describe('simulation slice', () => { }); }); + describe('setSnapInterfaceController', () => { + it('sets the snap interface controller', () => { + const snapInterfaceController = getSnapInterfaceController(); + + const result = reducer( + INITIAL_STATE, + setSnapInterfaceController(snapInterfaceController), + ); + + expect(result.snapInterfaceController).toStrictEqual( + snapInterfaceController, + ); + }); + }); + describe('setSourceCode', () => { it('sets the source code', () => { const result = reducer( @@ -97,4 +119,31 @@ describe('simulation slice', () => { expect(result.manifest?.result).toStrictEqual(MOCK_MANIFEST); }); }); + + describe('setSnapInterface', () => { + it('sets the snap interface', () => { + const snapInterface = { + id: 'foo', + snapId: 'bar' as SnapId, + content: Text({ children: 'Hello, world!' }), + context: null, + state: {}, + }; + const result = reducer(INITIAL_STATE, setSnapInterface(snapInterface)); + + expect(result.snapInterface).toStrictEqual(snapInterface); + }); + }); + + describe('setSnapInterfaceState', () => { + it('sets the snap interface state', () => { + const state = { foo: 'bar' }; + const result = reducer( + { ...INITIAL_STATE, snapInterface: { state: {} } as SnapInterface }, + setSnapInterfaceState(state), + ); + + expect(result.snapInterface?.state).toStrictEqual(state); + }); + }); }); diff --git a/packages/snaps-simulator/src/utils/render.test.tsx b/packages/snaps-simulator/src/utils/render.test.tsx new file mode 100644 index 0000000000..cdd27ef250 --- /dev/null +++ b/packages/snaps-simulator/src/utils/render.test.tsx @@ -0,0 +1,29 @@ +import { Bold, Text } from '@metamask/snaps-sdk/jsx'; +import { getJsxChildren } from '@metamask/snaps-utils'; + +import { SnapComponent } from '../features/renderer'; +import { renderTextChildren } from './render'; + +describe('renderTextChildren', () => { + it('returns a sting child', () => { + const element = Text({ children: 'Hello, world!' }); + + const result = renderTextChildren(getJsxChildren(element), 'test'); + + expect(result).toStrictEqual(['Hello, world!']); + }); + + it('returns a JSX child', () => { + const element = Text({ children: Bold({ children: 'Hello world' }) }); + + const result = renderTextChildren(getJsxChildren(element), 'test'); + + expect(result).toStrictEqual([ + , + ]); + }); +}); diff --git a/packages/snaps-utils/coverage.json b/packages/snaps-utils/coverage.json index 5c81ad80bf..242404e284 100644 --- a/packages/snaps-utils/coverage.json +++ b/packages/snaps-utils/coverage.json @@ -1,6 +1,6 @@ { - "branches": 96.56, - "functions": 98.4, - "lines": 98.52, - "statements": 94.71 + "branches": 96.82, + "functions": 98.8, + "lines": 98.87, + "statements": 95.02 } diff --git a/packages/snaps-utils/src/ui.test.tsx b/packages/snaps-utils/src/ui.test.tsx index 5f567d6cd0..298eb34265 100644 --- a/packages/snaps-utils/src/ui.test.tsx +++ b/packages/snaps-utils/src/ui.test.tsx @@ -42,6 +42,7 @@ import { walkJsx, getJsxChildren, serialiseJsx, + getJsxInterface, } from './ui'; describe('getTextChildren', () => { @@ -976,3 +977,31 @@ describe('serialiseJsx', () => { `); }); }); + +describe('getJsxInterface', () => { + it('returns the JSX interface for a JSX element', () => { + expect( + getJsxInterface( + + Hello + , + ), + ).toStrictEqual( + Box({ + children: Text({ + children: 'Hello', + }), + }), + ); + }); + + it('returns the JSX interface for a legacy element', () => { + expect(getJsxInterface(panel([text('Hello')]))).toStrictEqual( + Box({ + children: Text({ + children: 'Hello', + }), + }), + ); + }); +});