Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jest): 100% code coverage for Commands.test.ts #1455

Merged
merged 8 commits into from
Feb 14, 2022
38 changes: 38 additions & 0 deletions libraries/ui/Commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,42 @@ describe('Commands.isArgsValid', () => {
)
expect(result).toMatchSnapshot()
})

test('6', () => {
const object: any = [
{
name: '1.0.0',
options:
'data:image/svg+xml;charset=UTF-8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20version%3D%221.1%22%20baseProfile%3D%22full%22%20width%3D%22undefined%22%20height%3D%22undefined%22%3E%3Crect%20width%3D%22100%25%22%20height%3D%22100%25%22%20fill%3D%22grey%22%2F%3E%3Ctext%20x%3D%22NaN%22%20y%3D%22NaN%22%20font-size%3D%2220%22%20alignment-baseline%3D%22middle%22%20text-anchor%3D%22middle%22%20fill%3D%22white%22%3Eundefinedxundefined%3C%2Ftext%3E%3C%2Fsvg%3E',
},
]
const result: any = Commands.isArgsValid(
{ name: 'Edmond', description: 'Print Base', args: object },
['1.0.0', '^5.0.0', '^5.0.0', 'v4.0.0-rc.4', '4.0.0-beta1\t'],
)
expect(result).toMatchSnapshot()
})

describe('Commands.hasCommandPreview', () => {
test('0', () => {
const object: string = '/a'
/* Regex is [a-z0-9], hence everything that is in these is valid.
Other condition that we need to consider so that it passes the test is that:
it will return true if either the string has / at the start (text.charAt(0) === commandPrefix)
AND
the text is valid for the regex (/^[a-z0-9]+$/i) and has a length of 1 (/ab has a length of 2). */
const result: any = Commands.hasCommandPreview(object)
expect(result).toBeTruthy()
})
test('1', () => {
const object: string =
'ABC' /* A-Z is in [a-z0-9] because of the `i` flag at the end of the regex (/^[a-z0-9]+$/i)
Though what makes this string above invalid is because it does not adhere to both the condition of
text.charAt(0) === commandPrefix && (cmd.match(/^[a-z0-9]+$/i) || text.length === 1)
It does not have `/` at the start, hence not adhering to text.charAt(0) === commandPrefix.
And though it adheres to `(cmd.match(/^[a-z0-9]+$/i)`, it does not adhere to `text.length === 1` */
const result: any = Commands.hasCommandPreview(object)
expect(result).toBeFalsy()
})
})
})
1 change: 1 addition & 0 deletions libraries/ui/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const commandPrefix = '/'
*/
export function containsCommand(text: string) {
const cmd = text.split(' ')[0].replace(commandPrefix, '')

return (
text.charAt(0) === commandPrefix &&
(cmd.match(/^[a-z0-9]+$/i) || text.length === 1) // the || part is needed for showing all the available commands after writing the commandPrefix
Expand Down
2 changes: 2 additions & 0 deletions libraries/ui/__snapshots__/Commands.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ exports[`Commands.isArgsValid 4 1`] = `false`;

exports[`Commands.isArgsValid 5 1`] = `true`;

exports[`Commands.isArgsValid 6 1`] = `true`;

exports[`Commands.parseCommand 0 1`] = `
Object {
"args": Array [
Expand Down