This repository has been archived by the owner on Feb 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improvement(createwithuserthemes): add styled themeMap functionality,…
… tests This adds the actual functionality for createWithUserThemes in the styled package, including a couple tests to make sure it works
- Loading branch information
Showing
5 changed files
with
727 additions
and
458 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import withThemes from './withThemes'; | ||
|
||
const createWithUserThemes = ( | ||
WrappedComponent, | ||
defaultThemeMap = {} | ||
) => (themeMap) => { | ||
// Merge user theme map with defaults. | ||
const mergedThemeMap = { | ||
default: defaultThemeMap, | ||
...themeMap, | ||
}; | ||
|
||
return withThemes(mergedThemeMap)(WrappedComponent); | ||
}; | ||
|
||
/** @component */ | ||
export default createWithUserThemes; |
62 changes: 46 additions & 16 deletions
62
packages/styled/components/hoc/createWithUserThemes.test.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,31 +1,61 @@ | ||
import React from 'react'; | ||
import { mount } from 'enzyme'; | ||
import styled from 'styled-components'; | ||
import createWithUserThemes from './createWithUserThemes'; | ||
|
||
describe('withUserThemes', () => { | ||
const ThemeableComponent = ({ theme }) => ( | ||
<div> | ||
<span className={theme.testClass}>Content One</span> | ||
<span className={theme.testClass2}>Content Two</span> | ||
</div> | ||
); | ||
const ThemeableComponent = ({ theme }) => { | ||
const { | ||
Wrapper, | ||
ContentItem, | ||
} = theme; | ||
|
||
return ( | ||
<Wrapper> | ||
<ContentItem>Content One</ContentItem> | ||
<ContentItem>Content Two</ContentItem> | ||
</Wrapper> | ||
); | ||
}; | ||
const userThemes = { | ||
theme1: { | ||
testClass: 'theme1__testClass__12345', | ||
testClass2: 'theme1__testClass2__asdfg', | ||
default: { | ||
Wrapper: styled.div` | ||
background-color: blue; | ||
`, | ||
ContentItem: styled.span` | ||
color: green; | ||
`, | ||
}, | ||
theme2: { | ||
testClass: 'theme2__testClass__34567', | ||
testClass2: 'theme2__testClass2__qwert', | ||
theme1: { | ||
Wrapper: styled.div` | ||
background-color: orange; | ||
`, | ||
ContentItem: styled.span` | ||
color: black; | ||
`, | ||
}, | ||
}; | ||
const ThemedComponent = createWithUserThemes(ThemeableComponent)(userThemes); | ||
|
||
it('Should use a themeMap and themeName provided by a user', () => { | ||
const wrapper = mount(<ThemedComponent themeName="theme1" />); | ||
expect(wrapper.contains([ | ||
<span className="theme1__testClass__12345">Content One</span>, | ||
<span className="theme1__testClass2__asdfg">Content Two</span>, | ||
])).toBe(true); | ||
|
||
expect( | ||
wrapper.exists(`.${userThemes.theme1.Wrapper.styledComponentId}`) | ||
).toBe(true); | ||
expect( | ||
wrapper.exists(`.${userThemes.theme1.ContentItem.styledComponentId}`) | ||
).toBe(true); | ||
}); | ||
|
||
it('Should override default with user theme if a default key is used in user themeMap', () => { | ||
const wrapper = mount(<ThemedComponent />); | ||
|
||
expect( | ||
wrapper.exists(`.${userThemes.default.Wrapper.styledComponentId}`) | ||
).toBe(true); | ||
expect( | ||
wrapper.exists(`.${userThemes.default.ContentItem.styledComponentId}`) | ||
).toBe(true); | ||
}); | ||
}); |
Oops, something went wrong.