Skip to content

Commit

Permalink
add tests; add values
Browse files Browse the repository at this point in the history
  • Loading branch information
thatmattlove committed Jun 21, 2024
1 parent 5aed657 commit 5c0ff6d
Show file tree
Hide file tree
Showing 8 changed files with 7,241 additions and 5,077 deletions.
27 changes: 0 additions & 27 deletions lib/hygraph.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,5 @@
import { objectHasProperty } from "./generic";

import type { RichTextContent, ElementNode } from "@graphcms/rich-text-types";

function isEmptyElement(element: ElementNode): boolean {
for (const child of element.children) {
if (child.text === "" || child.text === null) {
return true;
}
}
return false;
}

export function isEmptyContent(content: RichTextContent | null | undefined): boolean {
if (content === null || typeof content === "undefined") {
return true;
}
if (Array.isArray(content)) {
for (const child of content) {
if (isEmptyElement(child)) {
return true;
}
}
} else {
return isEmptyContent(content.children);
}
return false;
}

type ReactTextNode = {
text: string;
};
Expand Down
43 changes: 43 additions & 0 deletions lib/type-guards.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,46 @@
export function isEmptyRichText<T>(obj: T): obj is T {
if (typeof obj === "object" && obj !== null && "raw" in obj) {
if (typeof obj.raw === "object" && obj.raw !== null && "children" in obj.raw) {
if (Array.isArray(obj.raw.children)) {
if (obj.raw.children.length === 0) {
return true;
}
if (obj.raw.children.length === 1) {
const child = obj.raw.children[0];
if (
typeof child === "object" &&
child !== null &&
"children" in child &&
Array.isArray(child.children)
) {
if (child.children.length === 0) {
return true;
}
if (
child.children.length === 1 &&
typeof child.children[0] === "object" &&
child.children[0] !== null &&
"text" in child.children[0]
) {
if (child.children[0].text === "") {
return true;
}
}
}
}
return false;
}
}
}
return false;
}

export function is<T>(obj: T): obj is NonNullable<T> {
if (isEmptyRichText(obj)) {
return false;
}
if (typeof obj === "object" && obj !== null) {
return Object.keys(obj).length >= 1;
}
return typeof obj !== "undefined" && obj !== null;
}
83 changes: 83 additions & 0 deletions lib/type-guarts.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { describe, expect, it } from "vitest";

import { isEmptyRichText, is } from "./type-guards";

describe("isEmptyRichText", () => {
it("should be true", () => {
const obj1 = {
raw: {
children: [
{
type: "paragraph",
children: [
{
text: "",
},
],
},
],
},
};
expect(isEmptyRichText(obj1)).toBe(true);

const obj2 = {
raw: {
children: [],
},
};
expect(isEmptyRichText(obj2)).toBe(true);

const obj3 = {
raw: {
children: [
{
type: "paragraph",
children: [],
},
],
},
};
expect(isEmptyRichText(obj3)).toBe(true);
});

it("should be false", () => {
const obj = {
raw: {
children: [
{
type: "paragraph",
children: [
{
text: "has stuff",
},
],
},
],
},
};
expect(isEmptyRichText(obj)).toBe(false);
expect(isEmptyRichText({})).toBe(false);
expect(isEmptyRichText(null)).toBe(false);
expect(isEmptyRichText([])).toBe(false);
expect(isEmptyRichText("")).toBe(false);
expect(isEmptyRichText(0)).toBe(false);
expect(isEmptyRichText(true)).toBe(false);
expect(isEmptyRichText({ key: "val" })).toBe(false);
expect(isEmptyRichText(NaN)).toBe(false);
expect(isEmptyRichText(Infinity)).toBe(false);
expect(isEmptyRichText(new Date())).toBe(false);
expect(isEmptyRichText(undefined)).toBe(false);
});
});

describe("is", () => {
it("should be true", () => {
expect(is({ key: "val" })).toBe(true);
expect(is("stuff")).toBe(true);
expect(is(1)).toBe(true);
});
it("should be false", () => {
expect(is({})).toBe(false);
expect(is(null)).toBe(false);
});
});
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stellaraf/site",
"version": "1.7.5",
"version": "1.7.7",
"private": true,
"type": "module",
"sideEffects": false,
Expand Down Expand Up @@ -63,7 +63,8 @@
"analyze:server": "BUNDLE_ANALYZE=server next build",
"analyze:browser": "BUNDLE_ANALYZE=browser next build",
"precommit": "pnpx lefthook run pre-commit",
"codegen": "graphql-codegen-esm --config codegen.ts --overwrite"
"codegen": "graphql-codegen-esm --config codegen.ts --overwrite",
"test": "vitest run"
},
"browserslist": {
"production": [
Expand Down Expand Up @@ -114,6 +115,7 @@
"eslint-plugin-react": "^7.33.2",
"prettier": "^3.0.3",
"raw-loader": "^4.0.2",
"typescript": "^5.2.2"
"typescript": "^5.2.2",
"vitest": "^1.6.0"
}
}
14 changes: 12 additions & 2 deletions pages/about.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { Box, type BoxProps, Flex, Heading } from "@chakra-ui/react";
import { useTitleCase } from "use-title-case";

import { EmployeeGrid, Hero, Callout, Testimonials, OfficeLocations } from "~/components";
import {
EmployeeGrid,
Hero,
Callout,
Testimonials,
OfficeLocations,
ContentSection,
Divider,
} from "~/components";
import { getLocationTime, getHolidays } from "~/lib/server";
import { pageQuery, employeesQuery, commonStaticPropsQuery, officeLocationsQuery } from "~/queries";
import { Stage, type AboutPageProps } from "~/types";
Expand All @@ -27,14 +35,16 @@ const Section = (props: React.PropsWithChildren<BoxProps & Pick<AboutPageProps,
const About: NextPage<AboutPageProps> = props => {
const { title, subtitle, body, contents, callout, employees, officeLocations, holidays } = props;

const [employeesSection, locationsSection] = contents;
const [employeesSection, valuesSection, locationsSection] = contents;

return (
<>
<Hero title={title} subtitle={subtitle} body={body} />
<Section title={employeesSection.title}>
<EmployeeGrid employees={employees} />
</Section>
<Divider right my={8} _light={{ opacity: 0 }} />
<ContentSection content={valuesSection} index={1} />
<Section title={locationsSection.title}>
<OfficeLocations
officeLocations={officeLocations}
Expand Down
Loading

0 comments on commit 5c0ff6d

Please sign in to comment.