Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link-redirect-fix #1187

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 13 additions & 5 deletions components/DocsHelp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@
import extractPathWithoutFragment from '~/lib/extractPathWithoutFragment';

interface DocsHelpProps {
markdownFile?: string;
fileRenderType?: '_indexmd' | 'indexmd' | 'tsx' | '_md';
}

export function DocsHelp({ markdownFile }: DocsHelpProps) {
export function DocsHelp({ fileRenderType }: DocsHelpProps) {

Check failure on line 9 in components/DocsHelp.tsx

View workflow job for this annotation

GitHub Actions / Code Quality Checks

Multiple spaces found before '}'

Check failure on line 9 in components/DocsHelp.tsx

View workflow job for this annotation

GitHub Actions / Code Quality Checks

Delete `·`
const router = useRouter();
const path = encodeURIComponent(router.pathname);
const [isFormOpen, setIsFormOpen] = useState(false);
const [feedbackStatus, setFeedbackStatus] = useState('');
const [isSubmitting, setIsSubmitting] = useState(false);
const [error, setError] = useState('');
const feedbackFormRef = useRef<HTMLFormElement>(null);

let gitredirect = '';
if (fileRenderType === 'tsx') {
gitredirect = `https://github.com/json-schema-org/website/blob/main/pages${extractPathWithoutFragment(router.asPath) + '/index.page.tsx'}`;
} else if (fileRenderType === '_indexmd') {
gitredirect = `https://github.com/json-schema-org/website/blob/main/pages${extractPathWithoutFragment(router.asPath) + '/_index.md'}`;
} else if (fileRenderType === 'indexmd') {
gitredirect = `https://github.com/json-schema-org/website/blob/main/pages${extractPathWithoutFragment(router.asPath) + '/index.md'}`;
} else {
gitredirect = `https://github.com/json-schema-org/website/blob/main/pages${extractPathWithoutFragment(router.asPath) + '.md'}`;
}
async function createFeedbackHandler(event: FormEvent) {
event.preventDefault();
const formData = new FormData(feedbackFormRef.current!);
Expand Down Expand Up @@ -301,7 +309,7 @@
target='_blank'
rel='noreferrer'
className='px-[16px] py-[8px] cursor-pointer border-solid border-[#aaaaaa] border rounded-md hover:bg-gray-200 dark:hover:bg-gray-600'
href={`https://github.com/json-schema-org/website/blob/main/pages${markdownFile ? (markdownFile === '_indexPage' ? extractPathWithoutFragment(router.asPath) + '/_index.md' : extractPathWithoutFragment(router.asPath) + '.md') : `/${path}/index.page.tsx`}`}
href={gitredirect}
data-test='edit-on-github-link'
>
<svg
Expand Down
70 changes: 42 additions & 28 deletions cypress/components/DocsHelp.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,50 +20,49 @@
describe('DocsHelp Component', () => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
let mockRouter: MockRouter;
const extractPathWithoutFragment = (path: any) => path.split('#')[0];
// Note: we are not using the mockRouter in this test file, but it is required to mock the router in the component file

beforeEach(() => {
const markdownFile = '_index';
const fileRenderType = 'indexmd';
mockRouter = mockNextRouter();
cy.viewport(1200, 800);
cy.mount(<DocsHelp markdownFile={markdownFile} />);
cy.mount(<DocsHelp fileRenderType={fileRenderType} />);
});

// should render the component correctly
it('should render the component correctly', () => {
// Check if the main component wrapper is present
cy.mount(<DocsHelp />);
cy.get(DOCS_HELP).should('exist');

// "Need Help?" header
cy.get('[data-test="need-help-heading"]')
.should('have.prop', 'tagName', 'H2')
.and('contains', /Need Help?/i);
.and('contain.text', 'Need Help?');

// Main feedback question
cy.get('[data-test="feedback-main-heading"]')
.should('have.prop', 'tagName', 'H3')
.and('contains', /Did you find these docs helpful?/i);
.and('contain.text', 'Did you find these docs helpful?');

// Feedback form element
cy.get(FEEDBACK_FORM).should('have.prop', 'tagName', 'FORM');

// "Help us improve" section header
cy.get('[data-test="contribute-docs-heading"]')
.should('have.prop', 'tagName', 'H3')
.and('contains', /Help us make our docs great!/i);
.and('contain.text', 'Help us make our docs great!');

// Contribution encouragement text
cy.get('[data-test="contribute-docs-description"]')
.should('have.prop', 'tagName', 'P')
.and(
'contains',
/At JSON Schema, we value docs contributions as much as every other type of contribution!/i,
);
.and('contain.text', 'At JSON Schema, we value docs contributions');

// "Edit on GitHub" link
cy.get('[data-test="edit-on-github-link"]')
.should('have.prop', 'tagName', 'A')
.and('contains', /Edit this page on Github/i);
.and('contain.text', 'Edit this page on Github');

// "Learn to contribute" link
cy.get('[data-test="learn-to-contribute-link"]')
Expand All @@ -73,20 +72,17 @@
'href',
'https://github.com/json-schema-org/website/blob/main/CONTRIBUTING.md',
)
.and('contains', /Learn how to contribute/i);
.and('contain.text', 'Learn how to contribute');

// "Still Need Help?" section header
cy.get('[data-test="additional-help-heading"]')
.should('have.prop', 'tagName', 'H3')
.and('contains', /Still Need Help?/i);
.and('contain.text', 'Still Need Help?');

// Additional help description
cy.get('[data-test="additional-help-description"]')
.should('have.prop', 'tagName', 'P')
.should(
'contains',
/Learning JSON Schema is often confusing, but don't worry, we are here to help!./i,
);
.and('contain.text', 'Learning JSON Schema is often confusing');

// GitHub community link
cy.get('[ data-test="ask-on-github-link"]')
Expand All @@ -96,7 +92,7 @@
'href',
'https://github.com/orgs/json-schema-org/discussions/new?category=q-a',
)
.and('contains', /Ask the community on GitHub/i);
.and('contain.text', 'Ask the community on GitHub');

// Slack community link
cy.get('[data-test="ask-on-slack-link"]')
Expand Down Expand Up @@ -237,15 +233,33 @@
});

// This test is to check component render correctly with different markdown files
it('should render component with different markdown files', () => {
/* Note: Already checking with _index markdown file in the first test case */

// render with _indexPage markdown file
const markdownFile = '_indexPage';
cy.mount(<DocsHelp markdownFile={markdownFile} />);
cy.get(DOCS_HELP).should('exist');

// render without any markdown file
cy.mount(<DocsHelp />);
});
it('should render component with different markdown files and validate gitredirect', () => {
const fileRenderTypes: ('tsx' | 'indexmd' | '_indexmd' | '_md')[] = [
'tsx',
'_indexmd',
'indexmd',
'_md',
];

fileRenderTypes.forEach((type) => {
let expectedGitRedirect = '';

if (type === 'tsx') {
expectedGitRedirect = `https://github.com/json-schema-org/website/blob/main/pages${extractPathWithoutFragment(mockRouter.asPath) + '/index.page.tsx'}`;
} else if (type === '_indexmd') {
expectedGitRedirect = `https://github.com/json-schema-org/website/blob/main/pages${extractPathWithoutFragment(mockRouter.asPath) + '/_index.md'}`;
} else if (type === 'indexmd') {
expectedGitRedirect = `https://github.com/json-schema-org/website/blob/main/pages${extractPathWithoutFragment(mockRouter.asPath) + '/index.md'}`;
} else {
expectedGitRedirect = `https://github.com/json-schema-org/website/blob/main/pages${extractPathWithoutFragment(mockRouter.asPath) + '.md'}`;
}
cy.mount(<DocsHelp fileRenderType={type} />);

cy.get('[data-test="edit-on-github-link"]').should(
'have.attr',
'href',
expectedGitRedirect,
);
});
})

Check failure on line 264 in cypress/components/DocsHelp.cy.tsx

View workflow job for this annotation

GitHub Actions / Code Quality Checks

Insert `;`
});
4 changes: 2 additions & 2 deletions pages/[slug].page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default function StaticMarkdownPage({
frontmatter: any;
content: any;
}) {
const markdownFile = '_index';
const fileRenderType = '_md';
const newTitle = 'JSON Schema - ' + frontmatter.title;
return (
<SectionContext.Provider value={frontmatter.section || null}>
Expand All @@ -31,7 +31,7 @@ export default function StaticMarkdownPage({
</Head>
<Headline1>{frontmatter.title}</Headline1>
<StyledMarkdown markdown={content} />
<DocsHelp markdownFile={markdownFile} />
<DocsHelp fileRenderType={fileRenderType} />
</SectionContext.Provider>
);
}
Expand Down
3 changes: 2 additions & 1 deletion pages/docs/index.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

export default function Welcome() {
const newTitle = 'Welcome';
const fileRenderType = 'tsx';
return (
<SectionContext.Provider value='docs'>
<Head>
Expand Down Expand Up @@ -56,7 +57,7 @@
link='/specification'
/>
</div>
<DocsHelp />
<DocsHelp fileRenderType={fileRenderType}/>

Check failure on line 60 in pages/docs/index.page.tsx

View workflow job for this annotation

GitHub Actions / Code Quality Checks

Insert `·`

Check failure on line 60 in pages/docs/index.page.tsx

View workflow job for this annotation

GitHub Actions / Code Quality Checks

A space is required before closing bracket
</SectionContext.Provider>
);
}
Expand Down
7 changes: 5 additions & 2 deletions pages/draft-05/index.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,17 @@
}: {
blocks: any;
frontmatter: any;
}) {
})

Check failure on line 35 in pages/draft-05/index.page.tsx

View workflow job for this annotation

GitHub Actions / Code Quality Checks

Delete `⏎⏎`

{
const fileRenderType = 'indexmd';
return (
<SectionContext.Provider value={null}>
<Headline1>{frontmatter.title}</Headline1>
<DocTable frontmatter={frontmatter} />
<StyledMarkdown markdown={blocks.index} />
<StyledMarkdown markdown={blocks.body} />
<DocsHelp />
<DocsHelp fileRenderType={fileRenderType} />
</SectionContext.Provider>
);
}
Expand Down
4 changes: 2 additions & 2 deletions pages/draft-06/[slug].page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default function StaticMarkdownPage({
frontmatter: any;
content: any;
}) {
const markdownFile = '_index';
const fileRenderType = '_md';
const newTitle = 'JSON Schema - ' + frontmatter.title;

return (
Expand All @@ -32,7 +32,7 @@ export default function StaticMarkdownPage({
</Head>
<Headline1>{frontmatter.title}</Headline1>
<StyledMarkdown markdown={content} />
<DocsHelp markdownFile={markdownFile} />
<DocsHelp fileRenderType={fileRenderType} />
</SectionContext.Provider>
);
}
Expand Down
3 changes: 2 additions & 1 deletion pages/draft-06/index.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ export default function ImplementationsPages({
blocks: any;
frontmatter: any;
}) {
const fileRenderType = 'indexmd';
return (
<SectionContext.Provider value={null}>
<Headline1>{frontmatter.title}</Headline1>
<DocTable frontmatter={frontmatter} />
<StyledMarkdown markdown={blocks.index} />
<DocsHelp />
<DocsHelp fileRenderType={fileRenderType} />
</SectionContext.Provider>
);
}
Expand Down
4 changes: 2 additions & 2 deletions pages/draft-07/[slug].page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default function StaticMarkdownPage({
frontmatter: any;
content: any;
}) {
const markdownFile = '_index';
const fileRenderType = '_md';
const newTitle = 'JSON Schema - ' + frontmatter.title;

return (
Expand All @@ -32,7 +32,7 @@ export default function StaticMarkdownPage({
</Head>
<Headline1>{frontmatter.title}</Headline1>
<StyledMarkdown markdown={content} />
<DocsHelp markdownFile={markdownFile} />
<DocsHelp fileRenderType={fileRenderType} />
</SectionContext.Provider>
);
}
Expand Down
3 changes: 2 additions & 1 deletion pages/draft-07/index.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ export default function ImplementationsPages({
blocks: any;
frontmatter: any;
}) {
const fileRenderType = 'indexmd';
return (
<SectionContext.Provider value={null}>
<Headline1>{frontmatter.title}</Headline1>
<DocTable frontmatter={frontmatter} />
<StyledMarkdown markdown={blocks.index} />
<DocsHelp />
<DocsHelp fileRenderType={fileRenderType} />
</SectionContext.Provider>
);
}
Expand Down
4 changes: 2 additions & 2 deletions pages/draft/2019-09/[slug].page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default function StaticMarkdownPage({
frontmatter: any;
content: any;
}) {
const markdownFile = '_index';
const fileRenderType = '_md';
const newTitle = 'JSON Schema - ' + frontmatter.title;

return (
Expand All @@ -32,7 +32,7 @@ export default function StaticMarkdownPage({
</Head>
<Headline1>{frontmatter.title}</Headline1>
<StyledMarkdown markdown={content} />
<DocsHelp markdownFile={markdownFile} />
<DocsHelp fileRenderType={fileRenderType} />
</SectionContext.Provider>
);
}
Expand Down
3 changes: 2 additions & 1 deletion pages/draft/2019-09/index.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ export default function ImplementationsPages({
blocks: any;
frontmatter: any;
}) {
const fileRenderType = 'indexmd';
return (
<SectionContext.Provider value={null}>
<Headline1>{frontmatter.title}</Headline1>
<DocTable frontmatter={frontmatter} />
<StyledMarkdown markdown={blocks.index} />
<DocsHelp />
<DocsHelp fileRenderType={fileRenderType} />
</SectionContext.Provider>
);
}
Expand Down
4 changes: 2 additions & 2 deletions pages/draft/2020-12/[slug].page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default function StaticMarkdownPage({
frontmatter: any;
content: any;
}) {
const markdownFile = '_index';
const fileRenderType = '_md';
const newTitle = 'JSON Schema - ' + frontmatter.title;

return (
Expand All @@ -32,7 +32,7 @@ export default function StaticMarkdownPage({
</Head>
<Headline1>{frontmatter.title}</Headline1>
<StyledMarkdown markdown={content} />
<DocsHelp markdownFile={markdownFile} />
<DocsHelp fileRenderType={fileRenderType} />
</SectionContext.Provider>
);
}
Expand Down
3 changes: 2 additions & 1 deletion pages/draft/2020-12/index.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ export default function ImplementationsPages({
blocks: any;
frontmatter: any;
}) {
const fileRenderType = 'indexmd';
return (
<SectionContext.Provider value={null}>
<Headline1>{frontmatter.title}</Headline1>
<DocTable frontmatter={frontmatter} />
<StyledMarkdown markdown={blocks.index} />
<DocsHelp />
<DocsHelp fileRenderType={fileRenderType} />
</SectionContext.Provider>
);
}
Expand Down
4 changes: 2 additions & 2 deletions pages/implementers/[slug].page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default function StaticMarkdownPage({
frontmatter: any;
content: any;
}) {
const markdownFile = '_index';
const fileRenderType = '_md';
const newTitle = 'JSON Schema - ' + frontmatter.title;
return (
<SectionContext.Provider value={frontmatter.section || null}>
Expand All @@ -31,7 +31,7 @@ export default function StaticMarkdownPage({
</Head>
<Headline1>{frontmatter.title}</Headline1>
<StyledMarkdown markdown={content} />
<DocsHelp markdownFile={markdownFile} />
<DocsHelp fileRenderType={fileRenderType} />
</SectionContext.Provider>
);
}
Expand Down
Loading
Loading