-
Notifications
You must be signed in to change notification settings - Fork 542
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor RPC method params and add tests
- Loading branch information
Showing
5 changed files
with
151 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import { | ||
getImplementation, | ||
getValidatedParams, | ||
NotificationType, | ||
} from './notify'; | ||
|
||
describe('snap_notify', () => { | ||
const validParams = { | ||
type: 'inApp', | ||
message: 'Some message', | ||
}; | ||
|
||
describe('getImplementation', () => { | ||
it('shows inApp notification', async () => { | ||
const showNativeNotification = jest.fn().mockResolvedValueOnce(true); | ||
const showInAppNotification = jest.fn().mockResolvedValueOnce(true); | ||
|
||
const notificationImplementation = getImplementation({ | ||
showNativeNotification, | ||
showInAppNotification, | ||
}); | ||
|
||
await notificationImplementation({ | ||
context: { | ||
origin: 'extension', | ||
}, | ||
method: '', | ||
params: { | ||
type: NotificationType.inApp, | ||
message: 'Some message', | ||
}, | ||
}); | ||
|
||
expect(showInAppNotification).toHaveBeenCalledWith('extension', { | ||
type: NotificationType.inApp, | ||
message: 'Some message', | ||
}); | ||
}); | ||
|
||
it('shows native notification', async () => { | ||
const showNativeNotification = jest.fn().mockResolvedValueOnce(true); | ||
const showInAppNotification = jest.fn().mockResolvedValueOnce(true); | ||
|
||
const notificationImplementation = getImplementation({ | ||
showNativeNotification, | ||
showInAppNotification, | ||
}); | ||
|
||
await notificationImplementation({ | ||
context: { | ||
origin: 'extension', | ||
}, | ||
method: '', | ||
params: { | ||
type: NotificationType.native, | ||
message: 'Some message', | ||
}, | ||
}); | ||
|
||
expect(showNativeNotification).toHaveBeenCalledWith('extension', { | ||
type: NotificationType.native, | ||
message: 'Some message', | ||
}); | ||
}); | ||
|
||
it('throws an error if the notification type is invalid', async () => { | ||
const showNativeNotification = jest.fn().mockResolvedValueOnce(true); | ||
const showInAppNotification = jest.fn().mockResolvedValueOnce(true); | ||
|
||
const notificationImplementation = getImplementation({ | ||
showNativeNotification, | ||
showInAppNotification, | ||
}); | ||
|
||
await expect( | ||
notificationImplementation({ | ||
context: { | ||
origin: 'extension', | ||
}, | ||
method: '', | ||
params: { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
type: 'invalid-type', | ||
message: 'Some message', | ||
}, | ||
}), | ||
).rejects.toThrow('Must specify a valid notification "type".'); | ||
}); | ||
}); | ||
|
||
describe('getValidatedParams', () => { | ||
it('throws an error if the params is not an object', () => { | ||
expect(() => getValidatedParams([])).toThrow( | ||
'Expected params to be a single object.', | ||
); | ||
}); | ||
|
||
it('throws an error if the type is missing from params object', () => { | ||
expect(() => | ||
getValidatedParams({ type: undefined, message: 'Something happened.' }), | ||
).toThrow('Must specify a valid notification "type".'); | ||
}); | ||
|
||
it('throws an error if the message is empty', () => { | ||
expect(() => getValidatedParams({ type: 'inApp', message: '' })).toThrow( | ||
'Must specify a non-empty string "message" less than 50 characters long.', | ||
); | ||
}); | ||
|
||
it('throws an error if the message is not a string', () => { | ||
expect(() => getValidatedParams({ type: 'inApp', message: 123 })).toThrow( | ||
'Must specify a non-empty string "message" less than 50 characters long.', | ||
); | ||
}); | ||
|
||
it('throws an error if the message is larger than 50 characters', () => { | ||
expect(() => | ||
getValidatedParams({ | ||
type: 'inApp', | ||
message: | ||
'test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg', | ||
}), | ||
).toThrow( | ||
'Must specify a non-empty string "message" less than 50 characters long.', | ||
); | ||
}); | ||
|
||
it('returns valid parameters', () => { | ||
expect(getValidatedParams(validParams)).toStrictEqual(validParams); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters