From 1ef2fc645593aa15a907d782660f68857fd22386 Mon Sep 17 00:00:00 2001 From: Hassan Malik Date: Fri, 13 Sep 2024 16:13:22 -0400 Subject: [PATCH] update tests --- .../src/restricted/notify.test.ts | 15 ++- packages/snaps-utils/src/ui.test.tsx | 103 ++++++++++++------ 2 files changed, 82 insertions(+), 36 deletions(-) diff --git a/packages/snaps-rpc-methods/src/restricted/notify.test.ts b/packages/snaps-rpc-methods/src/restricted/notify.test.ts index aeecba1a38..fc62b2854a 100644 --- a/packages/snaps-rpc-methods/src/restricted/notify.test.ts +++ b/packages/snaps-rpc-methods/src/restricted/notify.test.ts @@ -20,6 +20,7 @@ describe('snap_notify', () => { showInAppNotification: jest.fn(), isOnPhishingList: jest.fn(), maybeUpdatePhishingList: jest.fn(), + getSnap: jest.fn(), }; expect( @@ -41,6 +42,7 @@ describe('snap_notify', () => { const showNativeNotification = jest.fn().mockResolvedValueOnce(true); const showInAppNotification = jest.fn().mockResolvedValueOnce(true); const isOnPhishingList = jest.fn().mockResolvedValueOnce(false); + const getSnap = jest.fn(); const maybeUpdatePhishingList = jest.fn(); const notificationImplementation = getImplementation({ @@ -48,6 +50,7 @@ describe('snap_notify', () => { showInAppNotification, isOnPhishingList, maybeUpdatePhishingList, + getSnap, }); await notificationImplementation({ @@ -72,12 +75,14 @@ describe('snap_notify', () => { const showInAppNotification = jest.fn().mockResolvedValueOnce(true); const isOnPhishingList = jest.fn().mockResolvedValueOnce(false); const maybeUpdatePhishingList = jest.fn(); + const getSnap = jest.fn(); const notificationImplementation = getImplementation({ showNativeNotification, showInAppNotification, isOnPhishingList, maybeUpdatePhishingList, + getSnap, }); await notificationImplementation({ @@ -102,12 +107,14 @@ describe('snap_notify', () => { const showInAppNotification = jest.fn().mockResolvedValueOnce(true); const isOnPhishingList = jest.fn().mockResolvedValueOnce(false); const maybeUpdatePhishingList = jest.fn(); + const getSnap = jest.fn(); const notificationImplementation = getImplementation({ showNativeNotification, showInAppNotification, isOnPhishingList, maybeUpdatePhishingList, + getSnap, }); await notificationImplementation({ @@ -132,12 +139,14 @@ describe('snap_notify', () => { const showInAppNotification = jest.fn().mockResolvedValueOnce(true); const isOnPhishingList = jest.fn().mockResolvedValueOnce(false); const maybeUpdatePhishingList = jest.fn(); + const getSnap = jest.fn(); const notificationImplementation = getImplementation({ showNativeNotification, showInAppNotification, isOnPhishingList, maybeUpdatePhishingList, + getSnap, }); await expect( @@ -160,12 +169,14 @@ describe('snap_notify', () => { const showInAppNotification = jest.fn().mockResolvedValueOnce(true); const isOnPhishingList = jest.fn().mockResolvedValueOnce(true); const maybeUpdatePhishingList = jest.fn(); + const getSnap = jest.fn(); const notificationImplementation = getImplementation({ showNativeNotification, showInAppNotification, isOnPhishingList, maybeUpdatePhishingList, + getSnap, }); await expect( @@ -187,12 +198,14 @@ describe('snap_notify', () => { const showInAppNotification = jest.fn().mockResolvedValueOnce(true); const isOnPhishingList = jest.fn().mockResolvedValueOnce(true); const maybeUpdatePhishingList = jest.fn(); + const getSnap = jest.fn(); const notificationImplementation = getImplementation({ showNativeNotification, showInAppNotification, isOnPhishingList, maybeUpdatePhishingList, + getSnap, }); await expect( @@ -207,7 +220,7 @@ describe('snap_notify', () => { }, }), ).rejects.toThrow( - 'Invalid URL: Protocol must be one of: https:, mailto:.', + 'Invalid URL: Protocol must be one of: https:, mailto:, metamask:.', ); }); }); diff --git a/packages/snaps-utils/src/ui.test.tsx b/packages/snaps-utils/src/ui.test.tsx index 569ace6a65..29203ec14c 100644 --- a/packages/snaps-utils/src/ui.test.tsx +++ b/packages/snaps-utils/src/ui.test.tsx @@ -546,8 +546,8 @@ describe('validateLink', () => { it('passes for a valid link', () => { const fn = jest.fn().mockReturnValue(false); - expect(() => validateLink('https://foo.bar', fn)).not.toThrow(); - expect(() => validateLink('mailto:foo@bar.com', fn)).not.toThrow(); + expect(() => validateLink('https://foo.bar', fn, fn)).not.toThrow(); + expect(() => validateLink('mailto:foo@bar.com', fn, fn)).not.toThrow(); expect(fn).toHaveBeenCalledTimes(2); expect(fn).toHaveBeenCalledWith('foo.bar'); @@ -557,8 +557,8 @@ describe('validateLink', () => { it('throws an error for an invalid protocol', () => { const fn = jest.fn().mockReturnValue(false); - expect(() => validateLink('http://foo.bar', fn)).toThrow( - 'Invalid URL: Protocol must be one of: https:, mailto:.', + expect(() => validateLink('http://foo.bar', fn, fn)).toThrow( + 'Invalid URL: Protocol must be one of: https:, mailto:, metamask:.', ); expect(fn).not.toHaveBeenCalled(); @@ -567,7 +567,7 @@ describe('validateLink', () => { it('throws an error for an invalid URL', () => { const fn = jest.fn().mockReturnValue(false); - expect(() => validateLink('foo.bar', fn)).toThrow( + expect(() => validateLink('foo.bar', fn, fn)).toThrow( 'Invalid URL: Unable to parse URL.', ); @@ -577,9 +577,9 @@ describe('validateLink', () => { it('throws an error for a phishing link', () => { const fn = jest.fn().mockReturnValue(true); - expect(() => validateLink('https://test.metamask-phishing.io', fn)).toThrow( - 'Invalid URL: The specified URL is not allowed.', - ); + expect(() => + validateLink('https://test.metamask-phishing.io', fn, fn), + ).toThrow('Invalid URL: The specified URL is not allowed.'); expect(fn).toHaveBeenCalledTimes(1); expect(fn).toHaveBeenCalledWith('test.metamask-phishing.io'); @@ -589,7 +589,7 @@ describe('validateLink', () => { const fn = jest.fn().mockReturnValue(true); expect(() => - validateLink('mailto:foo@test.metamask-phishing.io', fn), + validateLink('mailto:foo@test.metamask-phishing.io', fn, fn), ).toThrow('Invalid URL: The specified URL is not allowed.'); expect(fn).toHaveBeenCalledTimes(1); @@ -600,27 +600,31 @@ describe('validateLink', () => { describe('validateTextLinks', () => { it('passes for valid links', () => { expect(() => - validateTextLinks('[test](https://foo.bar)', () => false), + validateTextLinks('[test](https://foo.bar)', () => false, jest.fn()), ).not.toThrow(); expect(() => - validateTextLinks('[test](mailto:foo@bar.baz)', () => false), + validateTextLinks('[test](mailto:foo@bar.baz)', () => false, jest.fn()), ).not.toThrow(); expect(() => - validateTextLinks('[](https://foo.bar)', () => false), + validateTextLinks('[](https://foo.bar)', () => false, jest.fn()), ).not.toThrow(); expect(() => - validateTextLinks('[[test]](https://foo.bar)', () => false), + validateTextLinks('[[test]](https://foo.bar)', () => false, jest.fn()), ).not.toThrow(); expect(() => - validateTextLinks('[test](https://foo.bar "foo bar baz")', () => false), + validateTextLinks( + '[test](https://foo.bar "foo bar baz")', + () => false, + jest.fn(), + ), ).not.toThrow(); expect(() => - validateTextLinks('', () => false), + validateTextLinks('', () => false, jest.fn()), ).not.toThrow(); expect(() => @@ -628,6 +632,7 @@ describe('validateTextLinks', () => { `[foo][1] [1]: https://foo.bar`, () => false, + jest.fn(), ), ).not.toThrow(); @@ -636,51 +641,66 @@ describe('validateTextLinks', () => { `[foo][1] [1]: https://foo.bar "foo bar baz"`, () => false, + jest.fn(), ), ).not.toThrow(); }); it('passes for non-links', () => { expect(() => - validateTextLinks('Hello **http://localhost:3000**', () => false), + validateTextLinks( + 'Hello **http://localhost:3000**', + () => false, + jest.fn(), + ), ).not.toThrow(); }); it('throws an error if an invalid link is found in text', () => { expect(() => validateTextLinks('[test](http://foo.bar)', () => false), - ).toThrow('Invalid URL: Protocol must be one of: https:, mailto:.'); + ).toThrow( + 'Invalid URL: Protocol must be one of: https:, mailto:, metamask:.', + ); expect(() => validateTextLinks('[[test]](http://foo.bar)', () => false), - ).toThrow('Invalid URL: Protocol must be one of: https:, mailto:.'); + ).toThrow( + 'Invalid URL: Protocol must be one of: https:, mailto:, metamask:.', + ); expect(() => validateTextLinks('', () => false)).toThrow( - 'Invalid URL: Protocol must be one of: https:, mailto:.', + 'Invalid URL: Protocol must be one of: https:, mailto:, metamask:.', ); expect(() => validateTextLinks('[test](http://foo.bar "foo bar baz")', () => false), - ).toThrow('Invalid URL: Protocol must be one of: https:, mailto:.'); + ).toThrow( + 'Invalid URL: Protocol must be one of: https:, mailto:, metamask:.', + ); expect(() => validateTextLinks('[foo][1]\n\n[1]: http://foo.bar', () => false), - ).toThrow('Invalid URL: Protocol must be one of: https:, mailto:.'); + ).toThrow( + 'Invalid URL: Protocol must be one of: https:, mailto:, metamask:.', + ); expect(() => validateTextLinks( `[foo][1]\n\n[1]: http://foo.bar "foo bar baz"`, () => false, ), - ).toThrow('Invalid URL: Protocol must be one of: https:, mailto:.'); - - expect(() => validateTextLinks('[test](#code)', () => false)).toThrow( - 'Invalid URL: Unable to parse URL.', + ).toThrow( + 'Invalid URL: Protocol must be one of: https:, mailto:, metamask:.', ); - expect(() => validateTextLinks('[test](foo.bar)', () => false)).toThrow( - 'Invalid URL: Unable to parse URL.', - ); + expect(() => + validateTextLinks('[test](#code)', () => false, jest.fn()), + ).toThrow('Invalid URL: Unable to parse URL.'); + + expect(() => + validateTextLinks('[test](foo.bar)', () => false, jest.fn()), + ).toThrow('Invalid URL: Unable to parse URL.'); }); }); @@ -699,7 +719,9 @@ describe('validateJsxLinks', () => { ])('does not throw for a safe JSX text component', async (element) => { const isOnPhishingList = () => false; - expect(() => validateJsxLinks(element, isOnPhishingList)).not.toThrow(); + expect(() => + validateJsxLinks(element, isOnPhishingList, jest.fn()), + ).not.toThrow(); }); it('does not throw for a JSX component with a link outside of a Link component', async () => { @@ -712,6 +734,7 @@ describe('validateJsxLinks', () => { https://foo.bar , isOnPhishingList, + jest.fn(), ), ).not.toThrow(); }); @@ -730,24 +753,34 @@ describe('validateJsxLinks', () => { ])('throws for an unsafe JSX text component', async (element) => { const isOnPhishingList = () => true; - expect(() => validateJsxLinks(element, isOnPhishingList)).toThrow( - 'Invalid URL: The specified URL is not allowed.', - ); + expect(() => + validateJsxLinks(element, isOnPhishingList, jest.fn()), + ).toThrow('Invalid URL: The specified URL is not allowed.'); }); it('throws if the protocol is not allowed', () => { const isOnPhishingList = () => false; expect(() => - validateJsxLinks(Foo, isOnPhishingList), - ).toThrow('Invalid URL: Protocol must be one of: https:, mailto:.'); + validateJsxLinks( + Foo, + isOnPhishingList, + jest.fn(), + ), + ).toThrow( + 'Invalid URL: Protocol must be one of: https:, mailto:, metamask:.', + ); }); it('throws if the URL cannot be parsed', () => { const isOnPhishingList = () => false; expect(() => - validateJsxLinks(Foo, isOnPhishingList), + validateJsxLinks( + Foo, + isOnPhishingList, + jest.fn(), + ), ).toThrow('Invalid URL: Unable to parse URL.'); }); });