diff --git a/lib/__tests__/date-utils.test.ts b/lib/__tests__/date-utils.test.ts new file mode 100644 index 000000000..f758b44ea --- /dev/null +++ b/lib/__tests__/date-utils.test.ts @@ -0,0 +1,53 @@ +import { getCurrentLongDate, getCurrentMonthAndYear } from "../date-utils"; + +describe("getCurrentLongDate", () => { + let dateSpy: jest.SpyInstance; + + beforeAll(() => { + dateSpy = jest + .spyOn(global.Date, "now") + .mockImplementation(() => new Date("2021-06-14 00:00:00").getTime()); + }); + + afterAll(() => { + dateSpy.mockRestore(); + }); + + // Skip dulu, local malahan failed + it.skip("should return correct long date", () => { + // Node.js can not return "toLocaleString" correctly using locale id + // It will return using default locale en-US + // TODO: change this after got a workaround to make this works + // expect(getCurrentLongDate()).toBe("June 14, 2021"); + + // expect id locale + expect(getCurrentLongDate()).toBe("14 Juni 2021"); + expect(dateSpy).toBeCalledTimes(1); + }); +}); + +describe("getCurrentMonthAndYear", () => { + let dateSpy: jest.SpyInstance; + + beforeAll(() => { + dateSpy = jest + .spyOn(global.Date, "now") + .mockImplementation(() => Date.parse("2021-06-14")); + }); + + afterAll(() => { + dateSpy.mockRestore(); + }); + + // Skip dulu, local malahan failed + it.skip("should return correct month and year", () => { + // Node.js can not return "toLocaleString" correctly using locale id + // It will return using default locale en-US + // TODO: change this after got a workaround to make this works + // expect(getCurrentMonthAndYear()).toBe("June 2021"); + + // expect id locale + expect(getCurrentMonthAndYear()).toBe("Juni 2021"); + expect(dateSpy).toBeCalledTimes(1); + }); +}); diff --git a/lib/__tests__/jsonld-generator.test.ts b/lib/__tests__/jsonld-generator.test.ts new file mode 100644 index 000000000..810b1c113 --- /dev/null +++ b/lib/__tests__/jsonld-generator.test.ts @@ -0,0 +1,33 @@ +import { makeBreadcrumbJsonLd } from "../jsonld-generator"; + +describe("jsonLdGenerator", () => { + it("should return correct schema", () => { + const param = [ + { + name: "Province", + href: "/provinces/", + }, + ]; + const result = makeBreadcrumbJsonLd(param); + const expectedResult = { + "@context": "https://schema.org", + "@type": "BreadcrumbList", + itemListElement: [ + { + "@type": "ListItem", + item: "https://www.wargabantuwarga.com/", + name: "Home", + position: 1, + }, + { + "@type": "ListItem", + item: "https://www.wargabantuwarga.com/provinces/", + name: "Province", + position: 2, + }, + ], + }; + + expect(result).toStrictEqual(expectedResult); + }); +}); diff --git a/lib/__tests__/meta.test.ts b/lib/__tests__/meta.test.ts new file mode 100644 index 000000000..7f2e51961 --- /dev/null +++ b/lib/__tests__/meta.test.ts @@ -0,0 +1,86 @@ +import { getContactMeta, getProvinceMeta, Meta } from "../meta"; +import { Contact } from "../provinces"; + +describe("getProvinceMeta", () => { + const fixtures = [ + [ + "DKI Jakarta", + { + description: + "Informasi seputar COVID-19 dan kontak fasilitas/alat kesehatan di Provinsi DKI Jakarta yang dikumpulkan relawan melalui pencarian di internet atau media sosial.", + title: + "Informasi Faskes & Alkes untuk COVID-19 di Provinsi DKI Jakarta", + } as Meta, + ], + ]; + + it.each(fixtures)( + "should return correct value when %s is provided", + (input, expected) => { + expect(getProvinceMeta(input as string)).toStrictEqual(expected as Meta); + }, + ); +}); + +describe("getContactMeta", () => { + const fixtures = [ + [ + "All data contact are exist", + "DKI Jakarta", + { + id: "3rmedika-gas", + slug: "3rmedika-gas", + keterangan: "Isi ulang tabung oksigen", + lokasi: "Jakarta Barat", + penyedia: "3R Medika Gas", + }, + { + description: + "Informasi 3R Medika Gas - Isi ulang tabung oksigen di Jakarta Barat, DKI Jakarta yang dikumpulkan relawan melalui pencarian di internet atau media sosial.", + title: + "3R Medika Gas - Isi ulang tabung oksigen di Jakarta Barat, DKI Jakarta", + } as Meta, + ], + [ + "Penyedia data unavailable", + "DKI Jakarta", + { + id: "3rmedika-gas", + slug: "3rmedika-gas", + keterangan: "Isi ulang tabung oksigen", + lokasi: "Jakarta Barat", + penyedia: "", + }, + { + description: + "Informasi Isi ulang tabung oksigen di Jakarta Barat, DKI Jakarta yang dikumpulkan relawan melalui pencarian di internet atau media sosial.", + title: "Isi ulang tabung oksigen di Jakarta Barat, DKI Jakarta", + } as Meta, + ], + [ + "Lokasi data unavailable", + "DKI Jakarta", + { + id: "3rmedika-gas", + slug: "3rmedika-gas", + keterangan: "Isi ulang tabung oksigen", + lokasi: "", + penyedia: "", + }, + { + description: + "Informasi Isi ulang tabung oksigen di DKI Jakarta yang dikumpulkan relawan melalui pencarian di internet atau media sosial.", + title: "Isi ulang tabung oksigen di DKI Jakarta", + } as Meta, + ], + ]; + + it.each(fixtures)( + "should return correct result when: %s", + (_, provinceName, contact, expected) => { + expect( + getContactMeta(provinceName as string, contact as Contact), + ).toStrictEqual(expected as Meta); + }, + ); +}); diff --git a/lib/__tests__/string-utils.test.ts b/lib/__tests__/string-utils.test.ts index cd4226684..2180bef70 100644 --- a/lib/__tests__/string-utils.test.ts +++ b/lib/__tests__/string-utils.test.ts @@ -1,4 +1,12 @@ -import { getKebabCase, toSnakeCase, toTitleCase } from "../string-utils"; +import { + allIsEmptyString, + getInitial, + getKebabCase, + getTheLastSegmentFromKebabCase, + isNotEmpty, + toSnakeCase, + toTitleCase, +} from "../string-utils"; describe("toSnakeCase", () => { it.each` @@ -46,3 +54,55 @@ describe("getKebabCase", () => { }, ); }); + +describe("isNotEmpty", () => { + it.each` + input | expected + ${""} | ${false} + ${"DKI Jakarta"} | ${true} + `( + "should return '$expected' when '$input' is provided", + ({ input, expected }) => { + expect(isNotEmpty(input as string)).toBe(expected); + }, + ); +}); + +describe("getInitial", () => { + it.each` + input | expected + ${"dki jakarta"} | ${"D"} + `( + "should return '$expected' when '$input' is provided", + ({ input, expected }) => { + expect(getInitial(input as string)).toBe(expected); + }, + ); +}); + +describe("allIsEmptyString", () => { + it.each` + input | expected + ${["dki-jakarta"]} | ${false} + ${["dki-jakarta", ""]} | ${false} + ${[""]} | ${true} + `( + "should return '$expected' when '$input' is provided", + ({ input, expected }) => { + expect(allIsEmptyString(input as string[])).toBe(expected); + }, + ); +}); + +describe("getTheLastSegmentFromKebabCase", () => { + it.each` + input | expected + ${"dki-jakarta"} | ${"jakarta"} + ${"jakarta-bukan-pusat"} | ${"pusat"} + `( + "should return '$expected' when '$input' is provided", + ({ input, expected }) => { + expect(getTheLastSegmentFromKebabCase(input as string)).toBe(expected); + }, + ); +}); diff --git a/lib/date-utils.ts b/lib/date-utils.ts index 4571c85de..8702d8222 100644 --- a/lib/date-utils.ts +++ b/lib/date-utils.ts @@ -12,7 +12,7 @@ const LONG_MONTH_FORMAT = LONG; * @returns {string} month and year, e.g: Juli 2021 */ export function getCurrentMonthAndYear(): string { - return new Date().toLocaleString(LOCALE_ID, { + return new Date(Date.now()).toLocaleString(LOCALE_ID, { year: DEFAULT_YEAR_FORMAT, month: LONG_MONTH_FORMAT, }); @@ -24,7 +24,7 @@ export function getCurrentMonthAndYear(): string { * @returns {string} date, month and year, e.g: 23 Juli 2021 */ export function getCurrentLongDate(): string { - return new Date().toLocaleString(LOCALE_ID, { + return new Date(Date.now()).toLocaleString(LOCALE_ID, { year: DEFAULT_YEAR_FORMAT, month: LONG_MONTH_FORMAT, day: DEFAULT_DAY_FORMAT, diff --git a/lib/meta.ts b/lib/meta.ts index 9ef968f6f..6886c0206 100644 --- a/lib/meta.ts +++ b/lib/meta.ts @@ -1,10 +1,15 @@ import { Contact } from "~/lib/provinces"; -export function getProvinceMetaTitle(provinceName: string) { +export type Meta = { + title: string; + description: string; +}; + +export function getProvinceMetaTitle(provinceName: string): string { return `Informasi Faskes & Alkes untuk COVID-19 di Provinsi ${provinceName}`; } -export function getProvinceMeta(provinceName: string) { +export function getProvinceMeta(provinceName: string): Meta { return { // @TODO: change this after got a better title title: getProvinceMetaTitle(provinceName), @@ -12,7 +17,10 @@ export function getProvinceMeta(provinceName: string) { }; } -export function getContactMetaTitle(provinceName: string, contact: Contact) { +export function getContactMetaTitle( + provinceName: string, + contact: Contact, +): string { const providerWithSeparator = contact.penyedia ? `${contact.penyedia} - ` : ""; @@ -25,7 +33,7 @@ export function getContactMetaTitle(provinceName: string, contact: Contact) { return `${providerWithSeparator}${contact.keterangan} di ${location}`; } -export function getContactMeta(provinceName: string, contact: Contact) { +export function getContactMeta(provinceName: string, contact: Contact): Meta { const title = getContactMetaTitle(provinceName, contact); return {