|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const rewire = require('rewire'); |
| 4 | + |
| 5 | +test('shortIntroduction function exists', () => { |
| 6 | + const file = rewire("./app.js"); |
| 7 | + const shortIntroduction = file.__get__('shortIntroduction'); |
| 8 | + expect(typeof shortIntroduction).toBe('function'); |
| 9 | +}); |
| 10 | + |
| 11 | +test('shortIntroduction function requires 3 arguments', () => { |
| 12 | + const file = fs.readFileSync(path.resolve(__dirname, './app.js'), 'utf8'); |
| 13 | + const shortIntroductionRegex = /function\s*shortIntroduction\s*\(\s*\w+\s*,\s*\w+\s*,\s*\w+\s*\)\s*{/gm; |
| 14 | + const hasProperParameters = shortIntroductionRegex.test(file.toString()); |
| 15 | + expect(hasProperParameters).toBeTruthy(); |
| 16 | +}); |
| 17 | + |
| 18 | +test("shortIntroduction function creates correct introduction", ()=>{ |
| 19 | + // read the content of the file |
| 20 | + const file = fs.readFileSync(path.resolve(__dirname, './app.js'), 'utf8'); |
| 21 | + |
| 22 | + // Use regex pattern to check for the correct console.log within the function using template literals |
| 23 | + const patternTemplate = /console\.log\(\s*`Hello! my name is \${\s*\w+\s*}, my profession is \${\s*\w+\s*}\. I am \${\s*\w+\s*} years old\.`\s*\)\s*;?/gm; |
| 24 | + |
| 25 | + // Use regex pattern to check for the correct console.log within the function using + operator |
| 26 | + const patternPlus = /console\.log\(\s*"Hello! my name is "\s*\+\s*\w+\s*\+\s*", my profession is "\s*\+\s*\w+\s*\+\s*". I am "\s*\+\s*\w+\s*\+\s*" years old."\s*\)\s*;?/gm; |
| 27 | + |
| 28 | + const hasCorrectConsoleLogTemplate = patternTemplate.test(file.toString()); |
| 29 | + const hasCorrectConsoleLogPlus = patternPlus.test(file.toString()); |
| 30 | + |
| 31 | + expect(hasCorrectConsoleLogTemplate || hasCorrectConsoleLogPlus).toBeTruthy(); |
| 32 | +}); |
| 33 | + |
| 34 | + |
0 commit comments