11import { describe , expect , it } from 'vitest' ;
22
3- import { StorybookError } from '../storybook-error' ;
3+ import { StorybookError , appendErrorRef } from '../storybook-error' ;
44
55describe ( 'StorybookError' , ( ) => {
66 class TestError extends StorybookError {
@@ -28,7 +28,7 @@ describe('StorybookError', () => {
2828 it ( 'should generate the correct message with internal documentation link' , ( ) => {
2929 const error = new TestError ( true ) ;
3030 const expectedMessage =
31- 'This is a test error.\n\nMore info: https://storybook.js.org/error/SB_TEST_CATEGORY_0123\n' ;
31+ 'This is a test error.\n\nMore info: https://storybook.js.org/error/SB_TEST_CATEGORY_0123?ref=error \n' ;
3232 expect ( error . message ) . toBe ( expectedMessage ) ;
3333 } ) ;
3434
@@ -45,14 +45,14 @@ describe('StorybookError', () => {
4545 it ( 'should generate the correct message with multiple external documentation links' , ( ) => {
4646 const error = new TestError ( [
4747 'https://example.com/docs/first-error' ,
48- 'https://example.com /docs/second-error' ,
48+ 'https://storybook.js.org /docs/second-error' ,
4949 ] ) ;
5050 expect ( error . message ) . toMatchInlineSnapshot ( `
5151 "This is a test error.
5252
5353 More info:
5454 - https://example.com/docs/first-error
55- - https://example.com /docs/second-error
55+ - https://storybook.js.org /docs/second-error?ref= error
5656 "
5757 ` ) ;
5858 } ) ;
@@ -62,3 +62,83 @@ describe('StorybookError', () => {
6262 expect ( error . documentation ) . toBe ( false ) ;
6363 } ) ;
6464} ) ;
65+
66+ describe ( 'appendErrorRef' , ( ) => {
67+ it ( 'should append ref=error to storybook.js.org URLs without query parameters' , ( ) => {
68+ const url = 'https://storybook.js.org/docs/example' ;
69+ const result = appendErrorRef ( url ) ;
70+ expect ( result ) . toBe ( 'https://storybook.js.org/docs/example?ref=error' ) ;
71+ } ) ;
72+
73+ it ( 'should append ref=error to storybook.js.org URLs with existing query parameters' , ( ) => {
74+ const url = 'https://storybook.js.org/docs/example?foo=bar' ;
75+ const result = appendErrorRef ( url ) ;
76+ expect ( result ) . toBe ( 'https://storybook.js.org/docs/example?foo=bar&ref=error' ) ;
77+ } ) ;
78+
79+ it ( 'should append ref=error to storybook.js.org URLs with multiple existing query parameters' , ( ) => {
80+ const url = 'https://storybook.js.org/docs/example?foo=bar&baz=qux' ;
81+ const result = appendErrorRef ( url ) ;
82+ expect ( result ) . toBe ( 'https://storybook.js.org/docs/example?foo=bar&baz=qux&ref=error' ) ;
83+ } ) ;
84+
85+ it ( 'should handle storybook.js.org URLs with hash fragments' , ( ) => {
86+ const url = 'https://storybook.js.org/docs/example#section' ;
87+ const result = appendErrorRef ( url ) ;
88+ expect ( result ) . toBe ( 'https://storybook.js.org/docs/example?ref=error#section' ) ;
89+ } ) ;
90+
91+ it ( 'should handle storybook.js.org URLs with query parameters and hash fragments' , ( ) => {
92+ const url = 'https://storybook.js.org/docs/example?foo=bar#section' ;
93+ const result = appendErrorRef ( url ) ;
94+ expect ( result ) . toBe ( 'https://storybook.js.org/docs/example?foo=bar&ref=error#section' ) ;
95+ } ) ;
96+
97+ it ( 'should not modify non-storybook.js.org URLs' , ( ) => {
98+ const url = 'https://example.com/docs/test-error' ;
99+ const result = appendErrorRef ( url ) ;
100+ expect ( result ) . toBe ( 'https://example.com/docs/test-error' ) ;
101+ } ) ;
102+
103+ it ( 'should not modify relative URLs' , ( ) => {
104+ const url = '/docs/example' ;
105+ const result = appendErrorRef ( url ) ;
106+ expect ( result ) . toBe ( '/docs/example' ) ;
107+ } ) ;
108+
109+ it ( 'should not modify empty string URLs' , ( ) => {
110+ const url = '' ;
111+ const result = appendErrorRef ( url ) ;
112+ expect ( result ) . toBe ( '' ) ;
113+ } ) ;
114+
115+ it ( 'should not modify other domain URLs' , ( ) => {
116+ const url = 'https://github.com/storybookjs/storybook/issues/123' ;
117+ const result = appendErrorRef ( url ) ;
118+ expect ( result ) . toBe ( 'https://github.com/storybookjs/storybook/issues/123' ) ;
119+ } ) ;
120+
121+ it ( 'should not append ref=error if it already exists in the URL' , ( ) => {
122+ const url = 'https://storybook.js.org/docs/example?ref=error' ;
123+ const result = appendErrorRef ( url ) ;
124+ expect ( result ) . toBe ( 'https://storybook.js.org/docs/example?ref=error' ) ;
125+ } ) ;
126+
127+ it ( 'should not append ref=error if it already exists with other parameters' , ( ) => {
128+ const url = 'https://storybook.js.org/docs/example?foo=bar&ref=error&baz=qux' ;
129+ const result = appendErrorRef ( url ) ;
130+ expect ( result ) . toBe ( 'https://storybook.js.org/docs/example?foo=bar&ref=error&baz=qux' ) ;
131+ } ) ;
132+
133+ it ( 'should not append ref=error if it already exists in URL with hash fragment' , ( ) => {
134+ const url = 'https://storybook.js.org/docs/example?ref=error#target' ;
135+ const result = appendErrorRef ( url ) ;
136+ expect ( result ) . toBe ( 'https://storybook.js.org/docs/example?ref=error#target' ) ;
137+ } ) ;
138+
139+ it ( 'should append ref=error before hash fragment when no existing ref parameter' , ( ) => {
140+ const url = 'https://storybook.js.org/docs/example#target' ;
141+ const result = appendErrorRef ( url ) ;
142+ expect ( result ) . toBe ( 'https://storybook.js.org/docs/example?ref=error#target' ) ;
143+ } ) ;
144+ } ) ;
0 commit comments