-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react): add usePrefix hook and examples (#9687)
* feat(Carbon-React): add usePrefix hook and examples * chore(test): update snapshots * chore(test): update snapshots * chore(test): remove test on prefix class * chore(test): add usePrefix unit tests Co-authored-by: Josh Black <josh@josh.black> Co-authored-by: Josefina Mancilla <32556167+jnm2377@users.noreply.github.com> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
- Loading branch information
1 parent
d577bc7
commit bd402ef
Showing
7 changed files
with
82 additions
and
16 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
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
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
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
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
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,40 @@ | ||
/** | ||
* Copyright IBM Corp. 2020 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import { cleanup, render } from '@testing-library/react'; | ||
import React from 'react'; | ||
import { usePrefix, PrefixContext } from '../usePrefix'; | ||
|
||
describe('usePrefix', () => { | ||
afterEach(cleanup); | ||
|
||
it('should emit the default prefix without context', () => { | ||
let value = null; | ||
|
||
function TestComponent() { | ||
value = usePrefix(); | ||
return null; | ||
} | ||
|
||
render(<TestComponent />); | ||
expect(value).toBe('bx'); | ||
}); | ||
|
||
it('should emit the prefix in context', () => { | ||
function TestComponent() { | ||
return <span data-testid="test">test</span>; | ||
} | ||
|
||
const { getByTestId } = render( | ||
<PrefixContext.Provider value="test"> | ||
<TestComponent /> | ||
</PrefixContext.Provider> | ||
); | ||
|
||
expect(getByTestId('test')).toHaveTextContent('test'); | ||
}); | ||
}); |
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,15 @@ | ||
/** | ||
* Copyright IBM Corp. 2016, 2018 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import { settings } from 'carbon-components'; | ||
import React from 'react'; | ||
|
||
export const PrefixContext = React.createContext(settings.prefix); | ||
|
||
export function usePrefix() { | ||
return React.useContext(PrefixContext); | ||
} |