Skip to content

Commit

Permalink
feat(jest): 100% code coverage for Commands.test.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
drepram committed Feb 11, 2022
1 parent 1bd02cd commit cbb3d08
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
33 changes: 33 additions & 0 deletions libraries/ui/Commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,37 @@ 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' // ABC is not in [a-z0-9], it would be in it if it was rather [a-zA-Z0-9].
const result: any = Commands.hasCommandPreview(object)
expect(result).toBeFalsy()
})
})
})
8 changes: 8 additions & 0 deletions libraries/ui/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ export const commandPrefix = '/'
*/
export function containsCommand(text: string) {
const cmd = text.split(' ')[0].replace(commandPrefix, '')
console.log(1, cmd, text)
console.log(
2,
text.charAt(0) === commandPrefix,
text.charAt(0),
commandPrefix,
)
console.log(3, cmd, text, cmd.match(/^[a-z0-9]+$/i), text.length === 1)
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

0 comments on commit cbb3d08

Please sign in to comment.