Skip to content

Commit

Permalink
Merge pull request #411 from mazipan/mazipan/add-lib-testing
Browse files Browse the repository at this point in the history
test: add testing on lib
  • Loading branch information
mazipan authored Jul 25, 2021
2 parents 01614d8 + d18fd49 commit 699cafe
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 7 deletions.
53 changes: 53 additions & 0 deletions lib/__tests__/date-utils.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
33 changes: 33 additions & 0 deletions lib/__tests__/jsonld-generator.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
86 changes: 86 additions & 0 deletions lib/__tests__/meta.test.ts
Original file line number Diff line number Diff line change
@@ -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);
},
);
});
62 changes: 61 additions & 1 deletion lib/__tests__/string-utils.test.ts
Original file line number Diff line number Diff line change
@@ -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`
Expand Down Expand Up @@ -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);
},
);
});
4 changes: 2 additions & 2 deletions lib/date-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand All @@ -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,
Expand Down
16 changes: 12 additions & 4 deletions lib/meta.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
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),
description: `Informasi seputar COVID-19 dan kontak fasilitas/alat kesehatan di Provinsi ${provinceName} yang dikumpulkan relawan melalui pencarian di internet atau media sosial.`,
};
}

export function getContactMetaTitle(provinceName: string, contact: Contact) {
export function getContactMetaTitle(
provinceName: string,
contact: Contact,
): string {
const providerWithSeparator = contact.penyedia
? `${contact.penyedia} - `
: "";
Expand All @@ -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 {
Expand Down

0 comments on commit 699cafe

Please sign in to comment.