From 5ea6746a8d645ed82337f07951ad92df6cac8354 Mon Sep 17 00:00:00 2001 From: MaoShizhong <122839503+MaoShizhong@users.noreply.github.com> Date: Tue, 4 Feb 2025 04:08:26 +0000 Subject: [PATCH] Add test preventing use of built-in String repeat method --- 02_repeatString/repeatString.spec.js | 7 +++++++ 02_repeatString/solution/repeatString-solution.spec.js | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/02_repeatString/repeatString.spec.js b/02_repeatString/repeatString.spec.js index 305bce2..9c56f2b 100644 --- a/02_repeatString/repeatString.spec.js +++ b/02_repeatString/repeatString.spec.js @@ -13,6 +13,13 @@ describe('repeatString', () => { test.skip('repeats the string 0 times', () => { expect(repeatString('bye', 0)).toEqual(''); }); + test.skip('does not use the built-in String repeat method', () => { + /* Even though there is a built-in String repeat method, + in this exercise specifically, we want you to practise using loops */ + jest.spyOn(String.prototype, 'repeat').mockName('Built-in String repeat method'); + repeatString("don't use the built-in repeat method!", 1); + expect(String.prototype.repeat).not.toHaveBeenCalled(); + }); test.skip('returns ERROR with negative numbers', () => { expect(repeatString('goodbye', -1)).toEqual('ERROR'); }); diff --git a/02_repeatString/solution/repeatString-solution.spec.js b/02_repeatString/solution/repeatString-solution.spec.js index a9ca13e..ffe38a8 100644 --- a/02_repeatString/solution/repeatString-solution.spec.js +++ b/02_repeatString/solution/repeatString-solution.spec.js @@ -13,6 +13,13 @@ describe('repeatString', () => { test('repeats the string 0 times', () => { expect(repeatString('bye', 0)).toEqual(''); }); + test('does not use the built-in String repeat method', () => { + /* Even though there is a built-in String repeat method, + in this exercise specifically, we want you to practise using loops */ + jest.spyOn(String.prototype, 'repeat').mockName('Built-in String repeat method'); + repeatString("don't use the built-in repeat method!", 1); + expect(String.prototype.repeat).not.toHaveBeenCalled(); + }); test('returns ERROR with negative numbers', () => { expect(repeatString('goodbye', -1)).toEqual('ERROR'); });