-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit test for the UpdateLinkAttribute
- Loading branch information
Showing
3 changed files
with
177 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 109 additions & 34 deletions
143
packages/block-library/src/button/test/update-link-attribute.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,109 @@ | ||
// npm run test:unit -- packages/block-library/src/button | ||
|
||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import ButtonEdit from '../edit'; // Import your ButtonEdit component | ||
|
||
// Mock the setAttributes function | ||
const mockSetAttributes = jest.fn(); | ||
|
||
describe('ButtonEdit Component', () => { | ||
it('should update link attributes correctly', () => { | ||
const attributes = { | ||
// Initialize your attributes as needed for the test | ||
}; | ||
|
||
const { updateLinkAttributes } = render( | ||
<ButtonEdit | ||
attributes={attributes} | ||
setAttributes={mockSetAttributes} | ||
// Add other required props here | ||
/> | ||
); | ||
|
||
// Call the updateLinkAttributes method with desired arguments | ||
updateLinkAttributes('newURL', true, true); | ||
|
||
// Assert that setAttributes was called with the expected parameters | ||
expect(mockSetAttributes).toHaveBeenCalledWith({ | ||
url: 'newURL', | ||
linkTarget: 'new_tab_target_value', // Replace with your expected value | ||
rel: 'rel_value', // Replace with your expected value | ||
}); | ||
}); | ||
}); | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { updateLinkAttributes } from '../utils'; | ||
|
||
describe( 'updateLinkAttributes method', () => { | ||
it( 'should correctly handle unassigned rel', () => { | ||
const options = { | ||
url: 'example.com', | ||
opensInNewTab: true, | ||
nofollow: false, | ||
}; | ||
|
||
const result = updateLinkAttributes(options); | ||
|
||
expect( result.url ).toEqual( 'http://example.com' ); | ||
expect( result.linkTarget ).toEqual( '_blank' ); | ||
expect( result.rel ).toEqual( 'noreferrer noopener' ); | ||
} ); | ||
|
||
it( 'should return empty rel value as undefined', () => { | ||
const options = { | ||
url: 'example.com', | ||
opensInNewTab: false, | ||
nofollow: false, | ||
}; | ||
|
||
const result = updateLinkAttributes(options); | ||
|
||
expect( result.url ).toEqual( 'http://example.com' ); | ||
expect( result.linkTarget ).toEqual( undefined ); | ||
expect( result.rel ).toEqual( undefined ); | ||
} ); | ||
|
||
it( 'should correctly handle rel with existing values', () => { | ||
const options = { | ||
url: 'example.com', | ||
opensInNewTab: true, | ||
nofollow: true, | ||
rel: 'rel_value', | ||
}; | ||
|
||
const result = updateLinkAttributes(options); | ||
|
||
expect( result.url ).toEqual( 'http://example.com' ); | ||
expect( result.linkTarget ).toEqual( '_blank' ); | ||
expect( result.rel ).toEqual( 'rel_value noreferrer noopener nofollow' ); | ||
} ); | ||
|
||
it( 'should correctly update link attributes with opensInNewTab', () => { | ||
const options = { | ||
url: 'example.com', | ||
opensInNewTab: true, | ||
nofollow: false, | ||
rel: 'rel_value', | ||
}; | ||
|
||
const result = updateLinkAttributes(options); | ||
|
||
expect( result.url ).toEqual( 'http://example.com' ); | ||
expect( result.linkTarget ).toEqual( '_blank' ); | ||
expect( result.rel ).toEqual( 'rel_value noreferrer noopener' ); | ||
} ); | ||
|
||
it ( 'should correctly update link attributes with nofollow', () => { | ||
const options = { | ||
url: 'example.com', | ||
opensInNewTab: false, | ||
nofollow: true, | ||
rel: 'rel_value', | ||
}; | ||
|
||
const result = updateLinkAttributes(options); | ||
|
||
expect( result.url ).toEqual( 'http://example.com' ); | ||
expect( result.linkTarget ).toEqual( undefined ); | ||
expect( result.rel ).toEqual( 'rel_value nofollow' ); | ||
} ); | ||
|
||
it ( 'should correctly handle rel with existing nofollow values and remove duplicates', () => { | ||
const options = { | ||
url: 'example.com', | ||
opensInNewTab: true, | ||
nofollow: true, | ||
rel: 'rel_value nofollow', | ||
}; | ||
|
||
const result = updateLinkAttributes(options); | ||
|
||
expect( result.url ).toEqual( 'http://example.com' ); | ||
expect( result.linkTarget ).toEqual( '_blank' ); | ||
expect( result.rel ).toEqual( 'rel_value nofollow noreferrer noopener' ); | ||
} ); | ||
|
||
it ( 'should correctly handle rel with existing new tab values and remove duplicates', () => { | ||
const options = { | ||
url: 'example.com', | ||
opensInNewTab: true, | ||
nofollow: false, | ||
rel: 'rel_value noreferrer noopener', | ||
}; | ||
|
||
const result = updateLinkAttributes(options); | ||
|
||
expect( result.url ).toEqual( 'http://example.com' ); | ||
expect( result.linkTarget ).toEqual( '_blank' ); | ||
expect( result.rel ).toEqual( 'rel_value noreferrer noopener' ); | ||
} ); | ||
} ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { prependHTTP } from '@wordpress/url'; | ||
|
||
/** | ||
* Constants | ||
*/ | ||
export const NEW_TAB_REL = 'noreferrer noopener'; | ||
export const NEW_TAB_TARGET = '_blank'; | ||
export const NOFOLLOW_REL = 'nofollow'; | ||
|
||
/** | ||
* Updates the link attributes. | ||
* | ||
* @param {Object} attributes The current block attributes. | ||
* @param {string} attributes.rel The current link rel attribute. | ||
* @param {string} attributes.url The current link url. | ||
* @param {boolean} attributes.opensInNewTab Whether the link should open in a new window. | ||
* @param {boolean} attributes.nofollow Whether the link should be marked as nofollow. | ||
*/ | ||
export function updateLinkAttributes( { rel = '', url = '', opensInNewTab, nofollow } ) { | ||
let newLinkTarget; | ||
// Since `rel` is editable attribute, we need to check for existing values and proceed accordingly. | ||
let updatedRel = rel; | ||
|
||
if ( opensInNewTab ) { | ||
newLinkTarget = NEW_TAB_TARGET; | ||
updatedRel = updatedRel?.includes( NEW_TAB_REL ) | ||
? updatedRel | ||
: updatedRel + ` ${ NEW_TAB_REL }`; | ||
} else { | ||
const relRegex = new RegExp( `\\b${ NEW_TAB_REL }\\s*`, 'g' ); | ||
updatedRel = updatedRel?.replace( relRegex, '' ).trim(); | ||
} | ||
|
||
if ( nofollow ) { | ||
updatedRel = updatedRel?.includes( NOFOLLOW_REL ) | ||
? updatedRel | ||
: updatedRel + ` ${ NOFOLLOW_REL }`; | ||
} else { | ||
const relRegex = new RegExp( `\\b${ NOFOLLOW_REL }\\s*`, 'g' ); | ||
updatedRel = updatedRel?.replace( relRegex, '' ).trim(); | ||
} | ||
|
||
return { | ||
url: prependHTTP( url ), | ||
linkTarget: newLinkTarget, | ||
rel: updatedRel || undefined, | ||
}; | ||
} |