From a8ac96d870698d8ee82e17897a4540862e3867b2 Mon Sep 17 00:00:00 2001 From: Douglas Greenshields Date: Thu, 9 Mar 2023 19:27:55 +0000 Subject: [PATCH 1/2] isolate tests in exercise --- 2-mandatory/2-financial-times.js | 34 +++++++++++++++++--------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/2-mandatory/2-financial-times.js b/2-mandatory/2-financial-times.js index 2ce6fb73..504451b0 100644 --- a/2-mandatory/2-financial-times.js +++ b/2-mandatory/2-financial-times.js @@ -37,23 +37,25 @@ function averageNumberOfCharacters(allArticleTitles) { /* ======= List of Articles - DO NOT MODIFY ===== */ -const ARTICLE_TITLES = [ - "Streaming wars drive media groups to spend more than $100bn on new content", - "Amazon Prime Video India country head: streaming is driving a TV revolution", - "Aerospace chiefs prepare for bumpy ride in recovery of long-haul flights", - "British companies look to muscle in on US retail investing boom", - "Libor to take firm step towards oblivion on New Year's Day", - "Audit profession unattractive to new recruits, says PwC boss", - "Chinese social media users blast Elon Musk over near miss in space", - "Companies raise over $12tn in 'blockbuster' year for global capital markets", - "The three questions that dominate investment", - "Brussels urges Chile's incoming president to endorse EU trade deal", -]; +function articleTitles() { + return [ + "Streaming wars drive media groups to spend more than $100bn on new content", + "Amazon Prime Video India country head: streaming is driving a TV revolution", + "Aerospace chiefs prepare for bumpy ride in recovery of long-haul flights", + "British companies look to muscle in on US retail investing boom", + "Libor to take firm step towards oblivion on New Year's Day", + "Audit profession unattractive to new recruits, says PwC boss", + "Chinese social media users blast Elon Musk over near miss in space", + "Companies raise over $12tn in 'blockbuster' year for global capital markets", + "The three questions that dominate investment", + "Brussels urges Chile's incoming president to endorse EU trade deal", + ]; +} /* ======= TESTS - DO NOT MODIFY ===== */ test("should only return potential headlines", () => { - expect(new Set(potentialHeadlines(ARTICLE_TITLES))).toEqual(new Set([ + expect(new Set(potentialHeadlines(articleTitles()))).toEqual(new Set([ "British companies look to muscle in on US retail investing boom", "Libor to take firm step towards oblivion on New Year's Day", "Audit profession unattractive to new recruits, says PwC boss", @@ -66,16 +68,16 @@ test("should return an empty array for empty input", () => { }); test("should return the title with the fewest words", () => { - expect(titleWithFewestWords(ARTICLE_TITLES)).toEqual("The three questions that dominate investment"); + expect(titleWithFewestWords(articleTitles())).toEqual("The three questions that dominate investment"); }); test("should only return headlines containing numbers", () => { - expect(new Set(headlinesWithNumbers(ARTICLE_TITLES))).toEqual(new Set([ + expect(new Set(headlinesWithNumbers(articleTitles()))).toEqual(new Set([ "Streaming wars drive media groups to spend more than $100bn on new content", "Companies raise over $12tn in 'blockbuster' year for global capital markets" ])); }); test("should return the average number of characters in a headline", () => { - expect(averageNumberOfCharacters(ARTICLE_TITLES)).toEqual(65); + expect(averageNumberOfCharacters(articleTitles())).toEqual(65); }); From 0a68d5fa7ad3eea203ccc3efe2d8b38616453a09 Mon Sep 17 00:00:00 2001 From: Douglas Greenshields Date: Sat, 11 Mar 2023 10:51:17 +0000 Subject: [PATCH 2/2] add expectations that arrays have not changed value in ft exercise --- 2-mandatory/2-financial-times.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/2-mandatory/2-financial-times.js b/2-mandatory/2-financial-times.js index 504451b0..e54d27c2 100644 --- a/2-mandatory/2-financial-times.js +++ b/2-mandatory/2-financial-times.js @@ -55,12 +55,14 @@ function articleTitles() { /* ======= TESTS - DO NOT MODIFY ===== */ test("should only return potential headlines", () => { - expect(new Set(potentialHeadlines(articleTitles()))).toEqual(new Set([ + const titles = articleTitles(); + expect(new Set(potentialHeadlines(titles))).toEqual(new Set([ "British companies look to muscle in on US retail investing boom", "Libor to take firm step towards oblivion on New Year's Day", "Audit profession unattractive to new recruits, says PwC boss", "The three questions that dominate investment" ])); + expectArrayIsUnchanged(titles, articleTitles()); }); test("should return an empty array for empty input", () => { @@ -68,16 +70,28 @@ test("should return an empty array for empty input", () => { }); test("should return the title with the fewest words", () => { + const titles = articleTitles(); expect(titleWithFewestWords(articleTitles())).toEqual("The three questions that dominate investment"); + expectArrayIsUnchanged(titles, articleTitles()); }); test("should only return headlines containing numbers", () => { - expect(new Set(headlinesWithNumbers(articleTitles()))).toEqual(new Set([ + const titles = articleTitles(); + expect(new Set(headlinesWithNumbers(titles))).toEqual(new Set([ "Streaming wars drive media groups to spend more than $100bn on new content", "Companies raise over $12tn in 'blockbuster' year for global capital markets" ])); + expectArrayIsUnchanged(titles, articleTitles()); }); test("should return the average number of characters in a headline", () => { - expect(averageNumberOfCharacters(articleTitles())).toEqual(65); + const titles = articleTitles(); + expect(averageNumberOfCharacters(titles)).toEqual(65); + expectArrayIsUnchanged(titles, articleTitles()); }); + +function expectArrayIsUnchanged(first, second) { + // arguments to functions should not be modified + // this expectation checks arrays have the same values, to make sure arguments have not been changed + expect(first).toEqual(second); +}