Skip to content

Commit

Permalink
add test for ScrapeValidatorFactory and its ListAm product
Browse files Browse the repository at this point in the history
  • Loading branch information
surenpoghosian committed Aug 9, 2024
1 parent a73fb38 commit d261359
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/__tests__/scrapeValidatorFactory.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import ScrapeValidatorFactory from '../ScrapeValidatorFactory';
import ListAm from '../ScrapeValidatorFactory/products/ListAm'
import MobileCentre from '../ScrapeValidatorFactory/products/MobileCentre';
import { ScrapeValidatorVariant } from "../configs/types";
import parse, { HTMLElement } from 'node-html-parser';
import { ScrapeType } from '../configs/types';

describe("ScrapeValidatorFactory", () => {
it("should create a ListAm validator when ScrapeValidatorVariant.LISTAM is provided", () => {
const validator = ScrapeValidatorFactory.createScrapeValidator(ScrapeValidatorVariant.LISTAM);
expect(validator).toBeInstanceOf(ListAm);
});

it("should create a MobileCentre validator when ScrapeValidatorVariant.MOBILECENTRE is provided", () => {
const validator = ScrapeValidatorFactory.createScrapeValidator(ScrapeValidatorVariant.MOBILECENTRE);
expect(validator).toBeInstanceOf(MobileCentre);
});

it("should throw an error when an unsupported ScrapeValidatorVariant is provided", () => {
expect(() => {
ScrapeValidatorFactory.createScrapeValidator("UNSUPPORTED_TYPE" as unknown as ScrapeValidatorVariant);
}).toThrowError("Unsupported scrape type: UNSUPPORTED_TYPE");
});});

describe("ListAm", () => {
const listAmValidator = new ListAm();

it("should return false if the HTML does not contain elements with class '.gl' when ScrapeType.LIST is used", () => {
const htmlContent = `<div class="no-gl"></div>`;
const root = parse(htmlContent) as HTMLElement;

const isValid = listAmValidator.validate(root, ScrapeType.LIST);
expect(isValid).toBe(false);
});

it("should return true if the HTML contains elements with class '.gl' when ScrapeType.LIST is used", () => {
const htmlContent = `<div class="gl"></div>`;
const root = parse(htmlContent) as HTMLElement;

const isValid = listAmValidator.validate(root, ScrapeType.LIST);
expect(isValid).toBe(true);
});

it("should throw an error if ScrapeType.ITEM is used", () => {
const htmlContent = `<div class="item"></div>`;
const root = parse(htmlContent) as HTMLElement;

expect(() => {
listAmValidator.validate(root, ScrapeType.ITEM);
}).toThrowError("ScrapeType.ITEM validator logic is not implemented!");
});
});

0 comments on commit d261359

Please sign in to comment.