-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: add test to useParagonTheme hook and paragon utils #525
Merged
adamstankiewicz
merged 2 commits into
openedx:ags/inject-theme-css
from
eduNEXT:ags/inject-theme-css
Dec 9, 2023
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { act, renderHook } from '@testing-library/react-hooks'; | ||
import useParagonTheme from './useParagonTheme'; | ||
|
||
describe('useParagonTheme', () => { | ||
it('should return an array with and object that indicates the theme status, and a function', () => { | ||
const config = { | ||
PARAGON_THEME_URLS: { | ||
core: { | ||
urls: { | ||
default: 'https://cdn.jsdelivr.net/npm/@edx/paragon@$paragonVersion/dist/core.min.css', | ||
}, | ||
}, | ||
defaults: { | ||
light: 'light', | ||
dark: 'dark', | ||
}, | ||
variants: { | ||
light: { | ||
urls: { | ||
default: 'https://cdn.jsdelivr.net/npm/@edx/paragon@$paragonVersion/dist/light.min.css', | ||
}, | ||
}, | ||
dark: { | ||
urls: { | ||
default: 'https://cdn.jsdelivr.net/npm/@edx/paragon@$paragonVersion/dist/dark.min.css', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const { result } = renderHook(() => useParagonTheme(config)); | ||
const createdLinksTag = document.head.querySelectorAll('link'); | ||
act(() => { createdLinksTag.forEach((link) => link.onload()); }); | ||
|
||
expect(result.current).toEqual([{ isThemeLoaded: true, themeVariant: 'light' }, expect.any(Function)]); | ||
}); | ||
}); |
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,95 @@ | ||
import { removeExistingLinks, getDefaultThemeVariant, handleVersionSubstitution } from './utils'; | ||
|
||
Object.defineProperty(window, 'matchMedia', { | ||
writable: true, | ||
value: jest.fn().mockImplementation(query => ({ | ||
matches: true, | ||
media: query, | ||
})), | ||
}); | ||
|
||
const mockGetItem = jest.fn(); | ||
Object.defineProperty(window, 'localStorage', { | ||
value: { | ||
getItem: mockGetItem, | ||
}, | ||
}); | ||
|
||
describe('removeExistingLinks', () => { | ||
afterEach(() => { | ||
document.head.innerHTML = ''; | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should remove all the links in the DOM', () => { | ||
document.head.innerHTML = ` | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@edx/paragon@$21.0.0/dist/core.min.css"> | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@edx/brand@$2.0.0Version/dist/core.min.css"> | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@edx/paragon@$21.0.0/dist/light.min.css"> | ||
`; | ||
|
||
removeExistingLinks(document.querySelectorAll('link')); | ||
expect(document.querySelectorAll('link').length).toBe(0); | ||
}); | ||
}); | ||
|
||
describe('getDefaultThemeVariant', () => { | ||
it('should return the theme variant according with system preference', () => { | ||
const variant = getDefaultThemeVariant({ | ||
themeVariantDefaults: { | ||
light: 'light', | ||
dark: 'dark', | ||
}, | ||
themeVariants: { | ||
light: { | ||
fileName: 'light.min.css', | ||
}, | ||
dark: { | ||
fileName: 'dark.min.css', | ||
}, | ||
}, | ||
}); | ||
expect(variant).toEqual({ metadata: { fileName: 'dark.min.css' }, name: 'dark' }); | ||
}); | ||
|
||
it('should return the theme variant according with local storage preference', () => { | ||
mockGetItem.mockImplementation(() => 'light'); | ||
const variant = getDefaultThemeVariant({ | ||
themeVariantDefaults: { | ||
light: 'light', | ||
dark: 'dark', | ||
}, | ||
themeVariants: { | ||
light: { | ||
fileName: 'light.min.css', | ||
}, | ||
dark: { | ||
fileName: 'dark.min.css', | ||
}, | ||
}, | ||
}); | ||
expect(variant).toEqual({ metadata: { fileName: 'light.min.css' }, name: 'light' }); | ||
}); | ||
|
||
it('should return the theme variant configuration as default', () => { | ||
const variant = getDefaultThemeVariant({ | ||
themeVariantDefaults: { | ||
light: 'light', | ||
}, | ||
themeVariants: { | ||
light: { | ||
fileName: 'light.min.css', | ||
}, | ||
}, | ||
}); | ||
expect(variant).toEqual({ metadata: { fileName: 'light.min.css' }, name: 'light' }); | ||
}); | ||
}); | ||
|
||
describe('handleVersionSubstitution', () => { | ||
it('should substitude the paragon version to use', () => { | ||
const config = { localVersion: '21.1.1', wildcardKeyword: 'alpha', url: 'https://cdn.jsdelivr.net/npm/@edx/paragon@alpha/dist/core.min.css' }; | ||
const newLink = handleVersionSubstitution(config); | ||
expect(newLink).toBe('https://cdn.jsdelivr.net/npm/@edx/paragon@21.1.1/dist/core.min.css'); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like the version in this
href
URL is incorrectly formatted, i.e.$2.0.0Version
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I fixed it, thanks