Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/lib/check-frontmatter-type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,17 @@ describe("checkFrontmatterType", () => {
});
});

describe("when postingCampaignUuid is undefined", () => {
const errorMessages = checkFrontmatterType({
...frontMatter,
postingCampaignUuid: undefined,
});

it("returns no errors", () => {
expect(errorMessages).toEqual([]);
});
});

describe("when postingCampaignUuid is string", () => {
const errorMessages = checkFrontmatterType({
...frontMatter,
Expand Down Expand Up @@ -178,6 +189,17 @@ describe("checkFrontmatterType", () => {
});
});

describe("when agreedPostingCampaignTerm is undefined", () => {
const errorMessages = checkFrontmatterType({
...frontMatter,
agreedPostingCampaignTerm: undefined,
});

it("returns no errors", () => {
expect(errorMessages).toEqual([]);
});
});

describe("when agreedPostingCampaignTerm is String", () => {
const errorMessages = checkFrontmatterType({
...frontMatter,
Expand Down
13 changes: 9 additions & 4 deletions src/lib/check-frontmatter-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ interface FrontMatter {
id: string | null;
organizationUrlName: string | null;
slide: boolean;
postingCampaignUuid: string | null;
agreedPostingCampaignTerm: boolean;
postingCampaignUuid: string | null | undefined;
agreedPostingCampaignTerm: boolean | undefined;
}

interface CheckType {
Expand Down Expand Up @@ -79,7 +79,9 @@ const checkPostingCampaignUuid: CheckType = {
getMessage: () => "posting_campaign_uuidは文字列で入力してください",
isValid: ({ postingCampaignUuid }) => {
return (
postingCampaignUuid === null || typeof postingCampaignUuid === "string"
postingCampaignUuid === undefined ||
postingCampaignUuid === null ||
typeof postingCampaignUuid === "string"
);
},
};
Expand All @@ -88,7 +90,10 @@ const checkAgreedPostingCampaignTerm: CheckType = {
getMessage: () =>
"agreed_posting_campaign_termの設定はtrue/falseで入力してください",
isValid: ({ agreedPostingCampaignTerm }) => {
return typeof agreedPostingCampaignTerm === "boolean";
return (
agreedPostingCampaignTerm === undefined ||
typeof agreedPostingCampaignTerm === "boolean"
);
},
};

Expand Down
8 changes: 4 additions & 4 deletions src/lib/entities/qiita-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export class QiitaItem {
public readonly itemPath: string;
public readonly slide: boolean;
public readonly ignorePublish: boolean;
public readonly postingCampaignUuid: string | null;
public readonly agreedPostingCampaignTerm: boolean;
public readonly postingCampaignUuid: string | null | undefined;
public readonly agreedPostingCampaignTerm: boolean | undefined;

constructor({
id,
Expand Down Expand Up @@ -53,8 +53,8 @@ export class QiitaItem {
itemPath: string;
slide: boolean;
ignorePublish: boolean;
postingCampaignUuid: string | null;
agreedPostingCampaignTerm: boolean;
postingCampaignUuid: string | null | undefined;
agreedPostingCampaignTerm: boolean | undefined;
}) {
this.id = id;
this.title = title;
Expand Down
89 changes: 89 additions & 0 deletions src/lib/file-system-repo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,95 @@ tags: []
});
});
});

describe("when posting campaign keys are absent in frontmatter", () => {
it("keeps them undefined", () => {
const instance = new FileSystemRepo({ dataRootDir: "data_root_dir" });
const basename = "abc";

const mockFs = fs as jest.Mocked<typeof fs>;
mockFs.readdir.mockResolvedValueOnce([`${basename}.md`] as any[]);
mockFs.readFile.mockResolvedValue(`---
id: this_is_id
tags: []
---`);

return instance.loadItemByBasename(basename).then((item) => {
expect(item?.postingCampaignUuid).toBeUndefined();
expect(item?.agreedPostingCampaignTerm).toBeUndefined();
});
});

it("does not detect modification against remote data with default values", () => {
const instance = new FileSystemRepo({ dataRootDir: "data_root_dir" });
const basename = "abc";

const mockFs = fs as jest.Mocked<typeof fs>;
mockFs.readdir.mockResolvedValueOnce([`${basename}.md`] as any[]);
mockFs.readFile.mockResolvedValueOnce(`---
title: title
tags: []
private: false
id: this_is_id
slide: false
---
body`);
mockFs.readFile.mockResolvedValueOnce(`---
title: title
tags: []
private: false
id: this_is_id
slide: false
posting_campaign_uuid: null
agreed_posting_campaign_term: false
---
body`);

return instance.loadItemByBasename(basename).then((item) => {
expect(item?.modified).toBe(false);
});
});
});

describe("when posting campaign keys are explicitly set in frontmatter", () => {
it("returns the explicit values", () => {
const instance = new FileSystemRepo({ dataRootDir: "data_root_dir" });
const basename = "abc";

const mockFs = fs as jest.Mocked<typeof fs>;
mockFs.readdir.mockResolvedValueOnce([`${basename}.md`] as any[]);
mockFs.readFile.mockResolvedValue(`---
id: this_is_id
tags: []
posting_campaign_uuid: abcde12345fghij67890
agreed_posting_campaign_term: true
---`);

return instance.loadItemByBasename(basename).then((item) => {
expect(item?.postingCampaignUuid).toBe("abcde12345fghij67890");
expect(item?.agreedPostingCampaignTerm).toBe(true);
});
});

it("returns null for an explicit null", () => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

posting_campaign_uuid がない場合に undefined にするテストも欲しいです

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tomoasleep
こちらのテストは describe("when posting campaign keys are absent in frontmatter") 内の it("keeps them undefined") で追加しています(キーなしのfrontmatterを読み込んで postingCampaignUuid / agreedPostingCampaignTermundefined になることを確認しています)。

describe("when posting campaign keys are absent in frontmatter", () => {
it("keeps them undefined", () => {
const instance = new FileSystemRepo({ dataRootDir: "data_root_dir" });
const basename = "abc";
const mockFs = fs as jest.Mocked<typeof fs>;
mockFs.readdir.mockResolvedValueOnce([`${basename}.md`] as any[]);
mockFs.readFile.mockResolvedValue(`---
id: this_is_id
tags: []
---`);
return instance.loadItemByBasename(basename).then((item) => {
expect(item?.postingCampaignUuid).toBeUndefined();
expect(item?.agreedPostingCampaignTerm).toBeUndefined();
});
});

もし意図されていたのが別のケースでしたら補足いただけると助かります!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

すみません、そっちのテストを見落としてました 🙏 であれば問題ないと思います。

const instance = new FileSystemRepo({ dataRootDir: "data_root_dir" });
const basename = "abc";

const mockFs = fs as jest.Mocked<typeof fs>;
mockFs.readdir.mockResolvedValueOnce([`${basename}.md`] as any[]);
mockFs.readFile.mockResolvedValue(`---
id: this_is_id
tags: []
posting_campaign_uuid: null
agreed_posting_campaign_term: false
---`);

return instance.loadItemByBasename(basename).then((item) => {
expect(item?.postingCampaignUuid).toBeNull();
expect(item?.agreedPostingCampaignTerm).toBe(false);
});
});
});
});

describe("loadItems", () => {
Expand Down
26 changes: 16 additions & 10 deletions src/lib/file-system-repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class FileContent {
public readonly rawBody: string;
public readonly slide: boolean;
public readonly ignorePublish: boolean;
public readonly postingCampaignUuid: string | null;
public readonly agreedPostingCampaignTerm: boolean;
public readonly postingCampaignUuid: string | null | undefined;
public readonly agreedPostingCampaignTerm: boolean | undefined;

constructor({
title,
Expand All @@ -40,8 +40,8 @@ class FileContent {
rawBody: string;
slide: boolean;
ignorePublish: boolean;
postingCampaignUuid: string | null;
agreedPostingCampaignTerm: boolean;
postingCampaignUuid: string | null | undefined;
agreedPostingCampaignTerm: boolean | undefined;
}) {
this.title = title;
this.tags = tags;
Expand All @@ -68,8 +68,8 @@ class FileContent {
organizationUrlName: data.organization_url_name,
slide: data.slide,
ignorePublish: data.ignorePublish ?? false,
postingCampaignUuid: data.posting_campaign_uuid ?? null,
agreedPostingCampaignTerm: data.agreed_posting_campaign_term ?? false,
postingCampaignUuid: data.posting_campaign_uuid,
agreedPostingCampaignTerm: data.agreed_posting_campaign_term,
});
}

Expand Down Expand Up @@ -138,8 +138,12 @@ class FileContent {
organization_url_name: this.organizationUrlName,
slide: this.slide,
ignorePublish: this.ignorePublish,
posting_campaign_uuid: this.postingCampaignUuid,
agreed_posting_campaign_term: this.agreedPostingCampaignTerm,
...(this.postingCampaignUuid !== undefined && {
posting_campaign_uuid: this.postingCampaignUuid,
}),
...(this.agreedPostingCampaignTerm !== undefined && {
agreed_posting_campaign_term: this.agreedPostingCampaignTerm,
}),
});
}

Expand All @@ -161,8 +165,10 @@ class FileContent {
this.rawBody === aFileContent.rawBody &&
this.slide === aFileContent.slide &&
this.ignorePublish === aFileContent.ignorePublish &&
this.postingCampaignUuid === aFileContent.postingCampaignUuid &&
this.agreedPostingCampaignTerm === aFileContent.agreedPostingCampaignTerm
(this.postingCampaignUuid ?? null) ===
(aFileContent.postingCampaignUuid ?? null) &&
(this.agreedPostingCampaignTerm ?? false) ===
(aFileContent.agreedPostingCampaignTerm ?? false)
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/lib/validators/item-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ interface Item {
tags: string[];
secret: boolean;
organizationUrlName: string | null;
postingCampaignUuid: string | null;
agreedPostingCampaignTerm: boolean;
postingCampaignUuid: string | null | undefined;
agreedPostingCampaignTerm: boolean | undefined;
}

interface Validator {
Expand Down
8 changes: 4 additions & 4 deletions src/qiita-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ export class QiitaApi {
isPrivate: boolean;
organizationUrlName: string | null;
slide: boolean;
postingCampaignUuid: string | null;
agreedPostingCampaignTerm: boolean;
postingCampaignUuid: string | null | undefined;
agreedPostingCampaignTerm: boolean | undefined;
}) {
const data = JSON.stringify({
body: rawBody,
Expand Down Expand Up @@ -271,8 +271,8 @@ export class QiitaApi {
isPrivate: boolean;
organizationUrlName: string | null;
slide: boolean;
postingCampaignUuid: string | null;
agreedPostingCampaignTerm: boolean;
postingCampaignUuid: string | null | undefined;
agreedPostingCampaignTerm: boolean | undefined;
}) {
const data = JSON.stringify({
body: rawBody,
Expand Down
2 changes: 1 addition & 1 deletion src/server/api/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ const itemsShow = async (req: Express.Request, res: Express.Response) => {
? {
title: campaign.title,
link_url: campaign.link_url,
agreed: item.agreedPostingCampaignTerm,
agreed: item.agreedPostingCampaignTerm ?? false,
}
: null;

Expand Down