Files
odin-javascript-exercises/02_repeatString/solution/repeatString-solution.spec.js
T

47 lines
2.1 KiB
JavaScript
Raw Normal View History

2023-02-01 18:53:54 -05:00
const repeatString = require('./repeatString-solution');
2022-02-20 14:07:44 -05:00
2023-02-01 18:53:54 -05:00
describe('repeatString', () => {
test('repeats the string', () => {
expect(repeatString('hey', 3)).toEqual('heyheyhey');
2023-01-21 12:53:41 -05:00
});
2023-02-01 18:58:58 -05:00
test('repeats the string many times', () => {
2024-01-07 23:31:14 +02:00
expect(repeatString('hello', 10)).toEqual('hellohellohellohellohellohellohellohellohellohello');
2023-01-21 12:53:41 -05:00
});
2023-02-01 18:58:58 -05:00
test('repeats the string 1 times', () => {
2024-01-07 23:31:14 +02:00
expect(repeatString('hi', 1)).toEqual('hi');
2023-01-21 12:53:41 -05:00
});
2023-02-01 18:58:58 -05:00
test('repeats the string 0 times', () => {
2024-01-07 23:31:14 +02:00
expect(repeatString('bye', 0)).toEqual('');
2023-01-21 12:53:41 -05:00
});
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();
});
2023-02-01 18:58:58 -05:00
test('returns ERROR with negative numbers', () => {
2024-01-07 23:31:14 +02:00
expect(repeatString('goodbye', -1)).toEqual('ERROR');
2023-01-21 12:53:41 -05:00
});
2023-02-01 18:58:58 -05:00
test('repeats the string a random amount of times', function () {
2023-01-21 12:53:41 -05:00
/*The number is generated by using Math.random to get a value from between
2022-02-20 14:07:44 -05:00
0 to 1, when this is multiplied by 1000 and rounded down with Math.floor it
equals a number between 0 to 999 (this number will change everytime you run
the test).*/
2023-01-21 12:53:41 -05:00
// DO NOT use Math.floor(Math.random() * 1000) in your code,
// this test generates a random number, then passes it into your code with a function parameter.
// If this doesn't make sense, you should go read about functions here: https://www.theodinproject.com/paths/foundations/courses/foundations/lessons/fundamentals-part-3
const number = Math.floor(Math.random() * 1000);
/*The .match(/((hey))/g).length is a regex that will count the number of heys
2022-02-20 14:07:44 -05:00
in the result, which if your function works correctly will equal the number that
was randomly generated. */
expect(repeatString('hey', number).match(/((hey))/g).length).toEqual(
2023-01-21 12:53:41 -05:00
number
);
});
2023-02-01 18:58:58 -05:00
test('works with blank strings', () => {
2023-02-01 18:53:54 -05:00
expect(repeatString('', 10)).toEqual('');
2023-01-21 12:53:41 -05:00
});
2022-02-20 14:07:44 -05:00
});