diff --git a/libraries/ui/Commands.test.ts b/libraries/ui/Commands.test.ts index a9af8659ba..717133e113 100644 --- a/libraries/ui/Commands.test.ts +++ b/libraries/ui/Commands.test.ts @@ -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() + }) + }) }) diff --git a/libraries/ui/Commands.ts b/libraries/ui/Commands.ts index 869c850148..b74a845f33 100644 --- a/libraries/ui/Commands.ts +++ b/libraries/ui/Commands.ts @@ -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 diff --git a/libraries/ui/__snapshots__/Commands.test.ts.snap b/libraries/ui/__snapshots__/Commands.test.ts.snap index 53fec42204..d610587f26 100644 --- a/libraries/ui/__snapshots__/Commands.test.ts.snap +++ b/libraries/ui/__snapshots__/Commands.test.ts.snap @@ -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 [