|
| 1 | +import { jsonFromTable } from "../src/index"; |
| 2 | + |
| 3 | +const html = `<table><tr><th>SN</th><th>Name</th></tr><tr><td>1</td><td>Roshan</td></tr><tr><td>2</td><td>John</td></tr></table>`; |
| 4 | + |
| 5 | +describe("jsonFromTable - errors", () => { |
| 6 | + it("should throw error if both html and url are not provided", () => { |
| 7 | + // @ts-ignore |
| 8 | + expect(() => jsonFromTable()).toThrowError(); |
| 9 | + }); |
| 10 | + |
| 11 | + it("should throw error if table selector is no found in html", () => { |
| 12 | + expect(() => jsonFromTable({ html, selector: ".random" })).toThrowError(); |
| 13 | + }); |
| 14 | + |
| 15 | + it("should not throw error if both select and html/url is valid", () => { |
| 16 | + expect(() => jsonFromTable({ html, selector: "table" })).not.toThrowError(); |
| 17 | + }); |
| 18 | +}); |
| 19 | + |
| 20 | +describe("jsonFromTable - html", () => { |
| 21 | + it("should return object from html table", () => { |
| 22 | + const obj = jsonFromTable({ html }); |
| 23 | + expect(obj).toEqual({ name: ["Roshan", "John"], sn: ["1", "2"] }); |
| 24 | + }); |
| 25 | + |
| 26 | + it("should return array from html table", () => { |
| 27 | + const arr = jsonFromTable({ html, format: "array" }); |
| 28 | + expect(arr).toEqual([ |
| 29 | + ["SN", ["1", "2"]], |
| 30 | + ["Name", ["Roshan", "John"]], |
| 31 | + ]); |
| 32 | + }); |
| 33 | + |
| 34 | + it("should return json string from html table", () => { |
| 35 | + const json = jsonFromTable({ html, format: "json" }); |
| 36 | + expect(json).toEqual(`{"sn":["1","2"],"name":["Roshan","John"]}`); |
| 37 | + }); |
| 38 | + |
| 39 | + it("should return raw headers and body from html table", () => { |
| 40 | + const raw = jsonFromTable({ html, format: "raw" }); |
| 41 | + expect(raw).toEqual({ |
| 42 | + body: [ |
| 43 | + ["1", "Roshan"], |
| 44 | + ["2", "John"], |
| 45 | + ], |
| 46 | + headers: ["SN", "Name"], |
| 47 | + }); |
| 48 | + }); |
| 49 | +}); |
0 commit comments