@@ -20,50 +20,49 @@ const FEEDBACK_FORM_GITHUB_SUCCESS_MESSAGE =
2020describe ( 'DocsHelp Component' , ( ) => {
2121 // eslint-disable-next-line @typescript-eslint/no-unused-vars
2222 let mockRouter : MockRouter ;
23+ const extractPathWithoutFragment = ( path : any ) => path . split ( '#' ) [ 0 ] ;
2324 // Note: we are not using the mockRouter in this test file, but it is required to mock the router in the component file
2425
2526 beforeEach ( ( ) => {
26- const markdownFile = '_index ' ;
27+ const fileRenderType = 'indexmd ' ;
2728 mockRouter = mockNextRouter ( ) ;
2829 cy . viewport ( 1200 , 800 ) ;
29- cy . mount ( < DocsHelp markdownFile = { markdownFile } /> ) ;
30+ cy . mount ( < DocsHelp fileRenderType = { fileRenderType } /> ) ;
3031 } ) ;
3132
3233 // should render the component correctly
3334 it ( 'should render the component correctly' , ( ) => {
3435 // Check if the main component wrapper is present
36+ cy . mount ( < DocsHelp /> ) ;
3537 cy . get ( DOCS_HELP ) . should ( 'exist' ) ;
3638
3739 // "Need Help?" header
3840 cy . get ( '[data-test="need-help-heading"]' )
3941 . should ( 'have.prop' , 'tagName' , 'H2' )
40- . and ( 'contains ' , / N e e d H e l p ? / i ) ;
42+ . and ( 'contain.text ' , ' Need Help?' ) ;
4143
4244 // Main feedback question
4345 cy . get ( '[data-test="feedback-main-heading"]' )
4446 . should ( 'have.prop' , 'tagName' , 'H3' )
45- . and ( 'contains ' , / D i d y o u f i n d t h e s e d o c s h e l p f u l ? / i ) ;
47+ . and ( 'contain.text ' , ' Did you find these docs helpful?' ) ;
4648
4749 // Feedback form element
4850 cy . get ( FEEDBACK_FORM ) . should ( 'have.prop' , 'tagName' , 'FORM' ) ;
4951
5052 // "Help us improve" section header
5153 cy . get ( '[data-test="contribute-docs-heading"]' )
5254 . should ( 'have.prop' , 'tagName' , 'H3' )
53- . and ( 'contains ' , / H e l p u s m a k e o u r d o c s g r e a t ! / i ) ;
55+ . and ( 'contain.text ' , ' Help us make our docs great!' ) ;
5456
5557 // Contribution encouragement text
5658 cy . get ( '[data-test="contribute-docs-description"]' )
5759 . should ( 'have.prop' , 'tagName' , 'P' )
58- . and (
59- 'contains' ,
60- / A t J S O N S c h e m a , w e v a l u e d o c s c o n t r i b u t i o n s a s m u c h a s e v e r y o t h e r t y p e o f c o n t r i b u t i o n ! / i,
61- ) ;
60+ . and ( 'contain.text' , 'At JSON Schema, we value docs contributions' ) ;
6261
6362 // "Edit on GitHub" link
6463 cy . get ( '[data-test="edit-on-github-link"]' )
6564 . should ( 'have.prop' , 'tagName' , 'A' )
66- . and ( 'contains ' , / E d i t t h i s p a g e o n G i t h u b / i ) ;
65+ . and ( 'contain.text ' , ' Edit this page on Github' ) ;
6766
6867 // "Learn to contribute" link
6968 cy . get ( '[data-test="learn-to-contribute-link"]' )
@@ -73,20 +72,17 @@ describe('DocsHelp Component', () => {
7372 'href' ,
7473 'https://github.com/json-schema-org/website/blob/main/CONTRIBUTING.md' ,
7574 )
76- . and ( 'contains ' , / L e a r n h o w t o c o n t r i b u t e / i ) ;
75+ . and ( 'contain.text ' , ' Learn how to contribute' ) ;
7776
7877 // "Still Need Help?" section header
7978 cy . get ( '[data-test="additional-help-heading"]' )
8079 . should ( 'have.prop' , 'tagName' , 'H3' )
81- . and ( 'contains ' , / S t i l l N e e d H e l p ? / i ) ;
80+ . and ( 'contain.text ' , ' Still Need Help?' ) ;
8281
8382 // Additional help description
8483 cy . get ( '[data-test="additional-help-description"]' )
8584 . should ( 'have.prop' , 'tagName' , 'P' )
86- . should (
87- 'contains' ,
88- / L e a r n i n g J S O N S c h e m a i s o f t e n c o n f u s i n g , b u t d o n ' t w o r r y , w e a r e h e r e t o h e l p ! ./ i,
89- ) ;
85+ . and ( 'contain.text' , 'Learning JSON Schema is often confusing' ) ;
9086
9187 // GitHub community link
9288 cy . get ( '[ data-test="ask-on-github-link"]' )
@@ -96,7 +92,7 @@ describe('DocsHelp Component', () => {
9692 'href' ,
9793 'https://github.com/orgs/json-schema-org/discussions/new?category=q-a' ,
9894 )
99- . and ( 'contains ' , / A s k t h e c o m m u n i t y o n G i t H u b / i ) ;
95+ . and ( 'contain.text ' , ' Ask the community on GitHub' ) ;
10096
10197 // Slack community link
10298 cy . get ( '[data-test="ask-on-slack-link"]' )
@@ -237,15 +233,51 @@ describe('DocsHelp Component', () => {
237233 } ) ;
238234
239235 // This test is to check component render correctly with different markdown files
240- it ( 'should render component with different markdown files' , ( ) => {
241- /* Note: Already checking with _index markdown file in the first test case */
242-
243- // render with _indexPage markdown file
244- const markdownFile = '_indexPage' ;
245- cy . mount ( < DocsHelp markdownFile = { markdownFile } /> ) ;
246- cy . get ( DOCS_HELP ) . should ( 'exist' ) ;
247-
248- // render without any markdown file
249- cy . mount ( < DocsHelp /> ) ;
236+ it ( 'should render component with different markdown files and validate gitredirect' , ( ) => {
237+ const fileRenderTypes : ( 'tsx' | 'indexmd' | '_indexmd' | '_md' ) [ ] = [
238+ 'tsx' ,
239+ '_indexmd' ,
240+ 'indexmd' ,
241+ '_md' ,
242+ ] ;
243+
244+ fileRenderTypes . forEach ( ( type ) => {
245+ let expectedGitRedirect = '' ;
246+ if ( typeof type === 'string' && type . startsWith ( 'https://' ) ) {
247+ expectedGitRedirect = type ;
248+ } else if ( type === 'tsx' ) {
249+ expectedGitRedirect = `https://github.com/json-schema-org/website/blob/main/pages${ extractPathWithoutFragment ( mockRouter . asPath ) + '/index.page.tsx' } ` ;
250+ } else if ( type === '_indexmd' ) {
251+ expectedGitRedirect = `https://github.com/json-schema-org/website/blob/main/pages${ extractPathWithoutFragment ( mockRouter . asPath ) + '/_index.md' } ` ;
252+ } else if ( type === 'indexmd' ) {
253+ expectedGitRedirect = `https://github.com/json-schema-org/website/blob/main/pages${ extractPathWithoutFragment ( mockRouter . asPath ) + '/index.md' } ` ;
254+ } else {
255+ expectedGitRedirect = `https://github.com/json-schema-org/website/blob/main/pages${ extractPathWithoutFragment ( mockRouter . asPath ) + '.md' } ` ;
256+ }
257+ cy . mount ( < DocsHelp fileRenderType = { type } /> ) ;
258+
259+ cy . get ( '[data-test="edit-on-github-link"]' ) . should (
260+ 'have.attr' ,
261+ 'href' ,
262+ expectedGitRedirect ,
263+ ) ;
264+ } ) ;
265+ const customLink = 'https://example.com/custom-docs' ;
266+ cy . mount ( < DocsHelp fileRenderType = { customLink } /> ) ;
267+ cy . get ( '[data-test="edit-on-github-link"]' ) . should (
268+ 'have.attr' ,
269+ 'href' ,
270+ customLink ,
271+ ) ;
272+ } ) ;
273+ // Check if the "Edit on GitHub" link is present when showEditOption is true
274+ it ( 'should render the "Edit on GitHub" link when showEditOption is true' , ( ) => {
275+ cy . mount ( < DocsHelp fileRenderType = 'indexmd' showEditOption = { true } /> ) ;
276+ cy . get ( '[data-test="edit-on-github-link"]' ) . should ( 'exist' ) ;
277+ } ) ;
278+ // Check if the "Edit on GitHub" link is not present when showEditOption is false
279+ it ( 'should not render the "Edit on GitHub" link when showEditOption is false' , ( ) => {
280+ cy . mount ( < DocsHelp fileRenderType = 'indexmd' showEditOption = { false } /> ) ;
281+ cy . get ( '[data-test="edit-on-github-link"]' ) . should ( 'not.exist' ) ;
250282 } ) ;
251283} ) ;
0 commit comments