Skip to content

Commit

Permalink
add tests and update coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeRx committed Jun 12, 2024
1 parent f92ab41 commit 727f8a0
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 8 deletions.
8 changes: 4 additions & 4 deletions packages/snaps-simulator/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
19 changes: 19 additions & 0 deletions packages/snaps-simulator/src/contexts/utils.test.ts
Original file line number Diff line number Diff line change
@@ -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' } });
});
});
49 changes: 49 additions & 0 deletions packages/snaps-simulator/src/features/simulation/slice.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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';

Expand All @@ -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(
Expand Down Expand Up @@ -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);
});
});
});
29 changes: 29 additions & 0 deletions packages/snaps-simulator/src/utils/render.test.tsx
Original file line number Diff line number Diff line change
@@ -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([
<SnapComponent
key={`test-text-child-0`}
id={`test-text-child-0`}
node={Bold({ children: 'Hello world' })}
/>,
]);
});
});
8 changes: 4 additions & 4 deletions packages/snaps-utils/coverage.json
Original file line number Diff line number Diff line change
@@ -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
}
29 changes: 29 additions & 0 deletions packages/snaps-utils/src/ui.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
walkJsx,
getJsxChildren,
serialiseJsx,
getJsxInterface,
} from './ui';

describe('getTextChildren', () => {
Expand Down Expand Up @@ -976,3 +977,31 @@ describe('serialiseJsx', () => {
`);
});
});

describe('getJsxInterface', () => {
it('returns the JSX interface for a JSX element', () => {
expect(
getJsxInterface(
<Box>
<Text>Hello</Text>
</Box>,
),
).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',
}),
}),
);
});
});

0 comments on commit 727f8a0

Please sign in to comment.