From cbe17de3b3d565443580817f3098cc27502fc464 Mon Sep 17 00:00:00 2001 From: Scott Nath Date: Thu, 26 Oct 2023 22:31:48 -0400 Subject: [PATCH] :white_check_mark: add deep testing to dev.to (#14) --- .storybook/preview.js | 4 +- src/devto/helpers/testing.js | 18 ++- src/devto/post/content.js | 10 +- src/devto/post/html.js | 10 +- src/devto/post/post.shared-spec.js | 47 +++++++ src/devto/post/post.stories.js | 58 +++++++- src/devto/post/post.test.js | 8 +- src/devto/typedefs.js | 67 +++++++++ src/devto/user/content.js | 68 ++++++---- src/devto/user/html.js | 2 +- src/devto/user/user.shared-spec.js | 95 +++++++++++++ src/devto/user/user.stories.js | 82 +++++++++-- src/devto/user/user.test.js | 204 ++++++++++++++++++++++++++-- src/github/styles/vars-global.css | 3 +- src/github/user/content.js | 3 +- src/github/user/user.shared-spec.js | 4 +- src/github/user/user.test.js | 10 +- src/mastodon/typedefs.js | 30 ++++ src/utils/index.js | 16 ++- 19 files changed, 665 insertions(+), 74 deletions(-) create mode 100644 src/devto/post/post.shared-spec.js create mode 100644 src/devto/typedefs.js create mode 100644 src/devto/user/user.shared-spec.js create mode 100644 src/mastodon/typedefs.js diff --git a/.storybook/preview.js b/.storybook/preview.js index b495817..6f54cf9 100644 --- a/.storybook/preview.js +++ b/.storybook/preview.js @@ -2,6 +2,7 @@ import { setCustomElementsManifest } from '@storybook/web-components'; import customElements from '../custom-elements.json'; import { globalTypesPrimer, decoratorsPrimer } from './primer-preview'; import { viewports } from './viewports'; +import { stringify, parseify } from '../src/utils'; import "./storybook.css"; setCustomElementsManifest(customElements); @@ -14,7 +15,8 @@ global.attrGen = (args) => Object.entries(args) .map(([key, value]) => `\n ${key}="${value}"`) .join(' '); -global.stringify = (obj) => JSON.stringify(obj).replace(/"/g, """) +global.stringify = stringify; +global.parseify = parseify; /** @type { import('@storybook/web-components').Preview } */ const preview = { diff --git a/src/devto/helpers/testing.js b/src/devto/helpers/testing.js index b5c2976..7b06a4a 100644 --- a/src/devto/helpers/testing.js +++ b/src/devto/helpers/testing.js @@ -20,6 +20,8 @@ export const generateMockResponse = (content, type='article', status=200) => { if (type === 'article') { // calls /articleS/:id (adds 's' to type) url += `${type}s/${content.id}`; + } else if (type === 'articles') { + url += `${type}/latest?per_page=1000&username=fake`; } else if (type === 'users') { if (!content.id) { url += `${type}/by_username?url=${content.username}`; @@ -30,9 +32,21 @@ export const generateMockResponse = (content, type='article', status=200) => { if (status === 404) { return { + url, + method: 'GET', status: 404, - error: "Not Found" + delay: 0, + response: { + status: 404, + error: "Not Found" + }, } } - return content + return { + url, + method: 'GET', + status: 200, + delay: 0, + response: content, + } } \ No newline at end of file diff --git a/src/devto/post/content.js b/src/devto/post/content.js index 80474ff..8b3a5fa 100644 --- a/src/devto/post/content.js +++ b/src/devto/post/content.js @@ -28,9 +28,10 @@ import { getApiUrl } from '../helpers/index.js'; * @ignore */ export const fetchPost = async (id) => { - const response = await fetch(`${getApiUrl()}/articles/${id}`, { + const options = { cache: 'no-cache', - }); + }; + const response = await fetch(`${getApiUrl()}/articles/${id}`, options); const repoJson = await response.json(); return repoJson; } @@ -42,9 +43,10 @@ export const fetchPost = async (id) => { * @ignore */ export const fetchUserPosts = async (username) => { - const articles = await fetch(`${getApiUrl()}/articles/latest?per_page=1000&username=${username?.toLowerCase()}`, { + const options = { cache: 'no-cache', - }); + }; + const articles = await fetch(`${getApiUrl()}/articles/latest?per_page=1000&username=${username?.toLowerCase()}`, options); const articlesJson = await articles.json(); return articlesJson; } diff --git a/src/devto/post/html.js b/src/devto/post/html.js index f3632b6..4fbecd2 100644 --- a/src/devto/post/html.js +++ b/src/devto/post/html.js @@ -7,12 +7,16 @@ * @memberof DEVUtils.post */ function html(content) { - if (content.error || !content.url || !content.title) { - return ''; + if (content.error) { + return ` +
+

${content.error}

+
+ ` } return ` - +