From 9316f9263147df2c9a673004a83bd583f9d9f013 Mon Sep 17 00:00:00 2001 From: djdembeck Date: Thu, 17 Aug 2023 15:38:01 -0500 Subject: [PATCH] test(helper): :white_check_mark: cleanupDescription test; test lint add timeout to fetch tests --- tests/audible/authors/scrape.test.ts | 4 +- tests/audible/books/api.test.ts | 8 +-- tests/audible/books/chapter.test.ts | 6 +-- tests/audible/books/scrape.test.ts | 10 ++-- tests/audible/books/stitch.test.ts | 6 +-- tests/datasets/helpers/authors.ts | 50 ++++++++++--------- .../authors/audible/ScrapeHelper.test.ts | 17 +++++-- tests/helpers/utils/UpdateScheduler.test.ts | 39 +++++++-------- .../helpers/utils/cleanupDescription.test.ts | 38 ++++++++++++++ 9 files changed, 113 insertions(+), 65 deletions(-) create mode 100644 tests/helpers/utils/cleanupDescription.test.ts diff --git a/tests/audible/authors/scrape.test.ts b/tests/audible/authors/scrape.test.ts index 416f3465..655f2995 100644 --- a/tests/audible/authors/scrape.test.ts +++ b/tests/audible/authors/scrape.test.ts @@ -15,7 +15,7 @@ describe('Audible Author HTML', () => { asin = 'B00G0WYW92' helper = new ScrapeHelper(asin, 'us') response = await helper.process() - }) + }, 10000) it('returned the correct asin', () => { expect(response.asin).toEqual(authorParsedB00G0WYW92.asin) @@ -51,7 +51,7 @@ describe('Audible Author HTML', () => { asin = 'B0034NFIOI' helper = new ScrapeHelper(asin, 'us') response = await helper.process() - }) + }, 10000) it('returned the correct asin', () => { expect(response.asin).toEqual(authorParsedB0034NFIOI.asin) diff --git a/tests/audible/books/api.test.ts b/tests/audible/books/api.test.ts index 2a5e137c..6f041815 100644 --- a/tests/audible/books/api.test.ts +++ b/tests/audible/books/api.test.ts @@ -25,7 +25,7 @@ describe('Audible API', () => { response = fetched.product // Make an object with the same keys as the response minimalResponse = setupMinimalResponse(response) - }) + }, 10000) it('returned the correct data', () => { expect(minimalResponse).toEqual(minimalB08G9PRS1K) @@ -41,7 +41,7 @@ describe('Audible API', () => { response = fetched.product // Make an object with the same keys as the response minimalResponse = setupMinimalResponse(response) - }) + }, 10000) it('returned the correct data', () => { expect(minimalResponse).toEqual(minimalB08C6YJ1LS) @@ -62,7 +62,7 @@ describe('Audible API', () => { "James Patterson's Detective Billy Harney is back, this time investigating murders in a notorious Chicago drug ring, which will lead him, his sister, and his new partner through a dangerous web of corrupt politicians, vengeful billionaires, and violent dark web conspiracies...." const image = 'https://m.media-amazon.com/images/I/91H9ynKGNwL.jpg' minimalParsed = setupMinimalParsed(B08C6YJ1LS.product, description, image, parsed.genres) - }) + }, 10000) it('returned the correct data', () => { expect(response).toEqual(minimalParsed) @@ -83,7 +83,7 @@ describe('Audible API', () => { 'Harry Potter has never even heard of Hogwarts when the letters start dropping on the doormat at number four, Privet Drive. Addressed in green ink on yellowish parchment with a purple seal, they are swiftly confiscated by his grisly aunt and uncle....' const image = 'https://m.media-amazon.com/images/I/91eopoUCjLL.jpg' minimalParsed = setupMinimalParsed(B017V4IM1G.product, description, image, parsed.genres) - }) + }, 10000) it('returned the correct data', () => { expect(response).toEqual(minimalParsed) diff --git a/tests/audible/books/chapter.test.ts b/tests/audible/books/chapter.test.ts index 26e65f31..9501be77 100644 --- a/tests/audible/books/chapter.test.ts +++ b/tests/audible/books/chapter.test.ts @@ -18,7 +18,7 @@ describe('Audible API', () => { const fetched = await helper.fetchChapter() if (!fetched) throw new Error('Parsed is undefined') response = fetched - }) + }, 10000) it('returned the correct data', () => { expect(response).toEqual(chapterResponseB017V4IM1G) @@ -35,7 +35,7 @@ describe('Audible API', () => { const parsed = await helper.parseResponse(fetched) if (!parsed) throw new Error('Parsed is undefined') response = parsed - }) + }, 10000) it('returned the correct data', () => { expect(response).toEqual(chapterParsed1721358595) @@ -50,7 +50,7 @@ describe('Audible API', () => { const fetched = await helper.fetchChapter() const parsed = await helper.parseResponse(fetched) response = parsed - }) + }, 10000) it('returned undefined', () => { expect(response).toBeUndefined() diff --git a/tests/audible/books/scrape.test.ts b/tests/audible/books/scrape.test.ts index da1dd28c..33f28b28 100644 --- a/tests/audible/books/scrape.test.ts +++ b/tests/audible/books/scrape.test.ts @@ -21,7 +21,7 @@ describe('Audible HTML', () => { const parsed = await helper.parseResponse(fetched) if (!parsed) throw new Error('Parsed is undefined') response = parsed - }) + }, 10000) it('returned the correct data', () => { expect(response).toEqual(parsedB08G9PRS1K) @@ -37,7 +37,7 @@ describe('Audible HTML', () => { const parsed = await helper.parseResponse(fetched) if (!parsed) throw new Error('Parsed is undefined') response = parsed - }) + }, 10000) it('returned the correct data', () => { expect(response).toEqual(parsedB017V4IM1G) @@ -54,7 +54,7 @@ describe('Audible HTML', () => { const parsed = await helper.parseResponse(fetched) if (!parsed) throw new Error('Parsed is undefined') response = parsed - }) + }, 10000) it('returned the correct data', () => { expect(response).toEqual(parsedB08C6YJ1LS) @@ -69,7 +69,7 @@ describe('Audible HTML', () => { helper = new ScrapeHelper(asin, 'us') const fetched = await helper.fetchBook() response = fetched - }) + }, 10000) it('returned undefined', () => { expect(response).toBeUndefined() @@ -84,7 +84,7 @@ describe('Audible HTML', () => { const fetched = await helper.fetchBook() const parsed = await helper.parseResponse(fetched) response = parsed - }) + }, 10000) it('returned undefined', () => { expect(response).toBeUndefined() diff --git a/tests/audible/books/stitch.test.ts b/tests/audible/books/stitch.test.ts index 1e828dfd..0bb1fa3c 100644 --- a/tests/audible/books/stitch.test.ts +++ b/tests/audible/books/stitch.test.ts @@ -28,7 +28,7 @@ describe('Audible API and HTML Parsing', () => { // Set variables chapters = chapterData response = newBook - }) + }, 10000) it('returned the correct data', () => { expect(response).toEqual(combinedB017V4IM1G) @@ -51,7 +51,7 @@ describe('Audible API and HTML Parsing', () => { // Set variables chapters = chapterData response = newBook - }) + }, 10000) it('returned the correct data', () => { expect(response).toEqual(combinedB08C6YJ1LS) @@ -74,7 +74,7 @@ describe('Audible API and HTML Parsing', () => { // Set variables chapters = chapterData response = newBook - }) + }, 10000) it('returned the correct data', () => { expect(response).toEqual(minimalB0036I54I6) diff --git a/tests/datasets/helpers/authors.ts b/tests/datasets/helpers/authors.ts index 637cc28b..440fdf60 100644 --- a/tests/datasets/helpers/authors.ts +++ b/tests/datasets/helpers/authors.ts @@ -58,40 +58,42 @@ const similar = [ } ] export const similarUnsorted = [ - { - asin: 'B00A8JBYDU', - name: 'Peter Nealen' - }, - { - asin: 'B00W2ZAK7E', - name: 'JN Chaney' - }, - { - asin: 'B00B1GNL4E', - name: 'Rick Partlow' - }, - { - asin: 'B001IGQXEW', - name: 'Karen Traviss' - }, - { - asin: 'B007E4W0GC', - name: 'Jonathan P. Brazee' - }, + { + asin: 'B00A8JBYDU', + name: 'Peter Nealen' + }, + { + asin: 'B00W2ZAK7E', + name: 'JN Chaney' + }, + { + asin: 'B00B1GNL4E', + name: 'Rick Partlow' + }, + { + asin: 'B001IGQXEW', + name: 'Karen Traviss' + }, + { + asin: 'B007E4W0GC', + name: 'Jonathan P. Brazee' + }, { asin: 'B000APARWG', name: 'Blaine Lee Pardoe' }, - { - asin: 'B005LW1K1A', - name: 'William S Frisbee Jr' - }, + { + asin: 'B005LW1K1A', + name: 'William S Frisbee Jr' + }, { asin: 'B08YC2Y6MV', name: 'Doc Spears' } ] +export const cleanupDescription = `JASON ANSPACH (1979- ) is the award-winning, Associated Press Best-selling author of Galaxy's Edge, Wayward Galaxy, and Forgotten Ruin. He is an American author raised in a military family (Go Army!) known for pulse-pounding military science fiction and adventurous space operas that deftly blend action, suspense, and comedy. Together with his wife, their seven (not a typo) children, and a border collie named Charlotte, Jason resides in Puyallup, Washington. He remains undefeated at arm wrestling against his entire family. Galaxy's Edge: Author website:` + export const parsedAuthor: ApiAuthorProfile = { asin, description, diff --git a/tests/helpers/authors/audible/ScrapeHelper.test.ts b/tests/helpers/authors/audible/ScrapeHelper.test.ts index ee98edb3..c94b7c83 100644 --- a/tests/helpers/authors/audible/ScrapeHelper.test.ts +++ b/tests/helpers/authors/audible/ScrapeHelper.test.ts @@ -7,7 +7,12 @@ import * as fetchPlus from '#helpers/utils/fetchPlus' import SharedHelper from '#helpers/utils/shared' import { regions } from '#static/regions' import { htmlResponseMinified, htmlResponseNameOnly } from '#tests/datasets/audible/authors/scrape' -import { genres, parsedAuthor, similarUnsorted } from '#tests/datasets/helpers/authors' +import { + cleanupDescription, + genres, + parsedAuthor, + similarUnsorted +} from '#tests/datasets/helpers/authors' jest.mock('#helpers/utils/fetchPlus') jest.mock('#helpers/utils/shared') @@ -57,7 +62,10 @@ describe('ScrapeHelper should', () => { test('parse response', async () => { const author = await helper.fetchAuthor() - await expect(helper.parseResponse(author)).resolves.toEqual(parsedAuthor) + await expect(helper.parseResponse(author)).resolves.toEqual({ + ...parsedAuthor, + description: cleanupDescription + }) }) test('return undefined if no dom for parse response', async () => { @@ -65,7 +73,10 @@ describe('ScrapeHelper should', () => { }) test('process author', async () => { - await expect(helper.process()).resolves.toEqual(parsedAuthor) + await expect(helper.process()).resolves.toEqual({ + ...parsedAuthor, + description: cleanupDescription + }) }) test('return description', () => { diff --git a/tests/helpers/utils/UpdateScheduler.test.ts b/tests/helpers/utils/UpdateScheduler.test.ts index 20ee5fdc..7df8adfd 100644 --- a/tests/helpers/utils/UpdateScheduler.test.ts +++ b/tests/helpers/utils/UpdateScheduler.test.ts @@ -16,6 +16,9 @@ import AuthorShowHelper from '#helpers/routes/AuthorShowHelper' import BookShowHelper from '#helpers/routes/BookShowHelper' import ChapterShowHelper from '#helpers/routes/ChapterShowHelper' import UpdateScheduler from '#helpers/utils/UpdateScheduler' +import { authorWithoutProjection } from '#tests/datasets/helpers/authors' +import { bookWithoutProjection } from '#tests/datasets/helpers/books' +import { chaptersWithoutProjection } from '#tests/datasets/helpers/chapters' type MockContext = { client: DeepMockProxy @@ -48,50 +51,42 @@ describe('UpdateScheduler should', () => { }) test('getAllAuthorAsins', async () => { - jest.spyOn(AuthorModel, 'find').mockResolvedValue([{ asin: 'B079LRSMNN', region: 'us' }] as any) - await expect(helper.getAllAuthorAsins()).resolves.toEqual([ - { asin: 'B079LRSMNN', region: 'us' } - ]) + jest.spyOn(AuthorModel, 'find').mockResolvedValue([authorWithoutProjection]) + await expect(helper.getAllAuthorAsins()).resolves.toEqual([authorWithoutProjection]) expect(AuthorModel.find).toHaveBeenCalledWith({}, projection) }) test('getAllBookAsins', async () => { - jest.spyOn(BookModel, 'find').mockResolvedValue([{ asin: 'B079LRSMNN', region: 'us' }] as any) - await expect(helper.getAllBookAsins()).resolves.toEqual([{ asin: 'B079LRSMNN', region: 'us' }]) + jest.spyOn(BookModel, 'find').mockResolvedValue([bookWithoutProjection]) + await expect(helper.getAllBookAsins()).resolves.toEqual([bookWithoutProjection]) expect(BookModel.find).toHaveBeenCalledWith({}, projection) }) test('getAllChapterAsins', async () => { - jest - .spyOn(ChapterModel, 'find') - .mockResolvedValue([{ asin: 'B079LRSMNN', region: 'us' }] as any) - await expect(helper.getAllChapterAsins()).resolves.toEqual([ - { asin: 'B079LRSMNN', region: 'us' } - ]) + jest.spyOn(ChapterModel, 'find').mockResolvedValue([chaptersWithoutProjection]) + await expect(helper.getAllChapterAsins()).resolves.toEqual([chaptersWithoutProjection]) expect(ChapterModel.find).toHaveBeenCalledWith({}, projection) }) test('updateAuthors', async () => { - jest.spyOn(AuthorModel, 'find').mockResolvedValue([{ asin: 'B079LRSMNN', region: 'us' }] as any) - jest.spyOn(AuthorShowHelper.prototype, 'handler').mockResolvedValue({} as any) + jest.spyOn(AuthorModel, 'find').mockResolvedValue([authorWithoutProjection]) + jest.spyOn(AuthorShowHelper.prototype, 'handler').mockResolvedValue(undefined) await expect(helper.updateAuthors()).resolves.toEqual(undefined) expect(AuthorModel.find).toHaveBeenCalledWith({}, projection) expect(AuthorShowHelper.prototype.handler).toHaveBeenCalledWith() }) test('updateBooks', async () => { - jest.spyOn(BookModel, 'find').mockResolvedValue([{ asin: 'B079LRSMNN', region: 'us' }] as any) - jest.spyOn(BookShowHelper.prototype, 'handler').mockResolvedValue({} as any) + jest.spyOn(BookModel, 'find').mockResolvedValue([bookWithoutProjection]) + jest.spyOn(BookShowHelper.prototype, 'handler').mockResolvedValue(undefined) await expect(helper.updateBooks()).resolves.toEqual(undefined) expect(BookModel.find).toHaveBeenCalledWith({}, projection) expect(BookShowHelper.prototype.handler).toHaveBeenCalledWith() }) test('updateChapters', async () => { - jest - .spyOn(ChapterModel, 'find') - .mockResolvedValue([{ asin: 'B079LRSMNN', region: 'us' }] as any) - jest.spyOn(ChapterShowHelper.prototype, 'handler').mockResolvedValue({} as any) + jest.spyOn(ChapterModel, 'find').mockResolvedValue([chaptersWithoutProjection]) + jest.spyOn(ChapterShowHelper.prototype, 'handler').mockResolvedValue(undefined) await expect(helper.updateChapters()).resolves.toEqual(undefined) expect(ChapterModel.find).toHaveBeenCalledWith({}, projection) expect(ChapterShowHelper.prototype.handler).toHaveBeenCalledWith() @@ -125,7 +120,9 @@ describe('UpdateScheduler should', () => { }) test('updateAllJob', async () => { - jest.spyOn(helper, 'updateAllTask').mockReturnValue({} as any) + jest + .spyOn(helper, 'updateAllTask') + .mockReturnValue(new AsyncTask('id_1', async () => undefined)) expect(JSON.stringify(helper.updateAllJob())).toEqual( JSON.stringify( new LongIntervalJob({ days: 1, runImmediately: true }, helper.updateAllTask(), { diff --git a/tests/helpers/utils/cleanupDescription.test.ts b/tests/helpers/utils/cleanupDescription.test.ts new file mode 100644 index 00000000..8817b207 --- /dev/null +++ b/tests/helpers/utils/cleanupDescription.test.ts @@ -0,0 +1,38 @@ +import cleanupDescription from '#helpers/utils/cleanupDescription' +import { parsedAuthor } from '#tests/datasets/helpers/authors' +import { parsedBook } from '#tests/datasets/helpers/books' + +const badInput = ` +"Matthew Iden is the author of the psychological suspense novel The Winter Over, a half-dozen books in the Marty Singer detective series, and several acclaimed stand-alone novels. His eclectic work resume includes jobs with the US Postal Service, an international nonprofit, the Forest Service in Sitka, Alaska, and the globe-spanning Semester at Sea program. His latest, Birthday Girl, is a suspense novel featuring Elliott Nash, a former forensic psychologist who is now homeless but is recruited to save a young girl from a serial kidnapper (March 2018). Get in touch via e-mail at matt.iden@matthew-iden.com, Facebook at www.facebook.com/matthew.iden, Twitter @CrimeRighter, or visit www.matthew-iden.com for information on upcoming appearances, new releases, and to receive a free copy of the Marty Singer short story "The Guardian"--not available anywhere else."` + +const badAuthorInput = parsedAuthor.description as string + +const goodInput = parsedBook.description as string + +describe('cleanupDescription should', () => { + test('remove emails', () => { + const result = cleanupDescription(badInput) + expect(result).not.toContain('matt.iden@matthew-iden.com') + }) + + test('remove twitter handles', () => { + const result = cleanupDescription(badInput) + expect(result).not.toContain('@CrimeRighter') + }) + + test('remove urls', () => { + const result = cleanupDescription(badInput) + expect(result).not.toContain('www.facebook.com/matthew.iden') + + const result2 = cleanupDescription(badAuthorInput) + expect(result2).not.toContain('www.JasonAnspach.com') + expect(result2).not.toContain('www.InTheLegion.com') + expect(result2).not.toContain('facebook.com/authorjasonanspach') + expect(result2).not.toContain('twitter.com/TheJasonAnspach') + }) + + test('not remove any text from a good input', () => { + const result = cleanupDescription(goodInput) + expect(result).toEqual(goodInput) + }) +})