Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
JiuqingSong committed Jun 6, 2024
1 parent 31c48ea commit 1b67dd6
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,21 @@ describe('DarkColorHandlerImpl.ctor', () => {
);
});

it('with knownColors', () => {
it('with knownColors and generateColorKey', () => {
const div = document.createElement('div');
const mockedGetDarkColor = jasmine.createSpy('getDarkColor');
const mockedKnownColors = 'KNOWN' as any;
const handler = createDarkColorHandler(div, mockedGetDarkColor, mockedKnownColors);
const mockedGenerateColorKey = 'KEY' as any;
const handler = createDarkColorHandler(
div,
mockedGetDarkColor,
mockedKnownColors,
mockedGenerateColorKey
);

expect(handler.knownColors).toEqual(mockedKnownColors);
expect(handler.getDarkColor).toBe(mockedGetDarkColor);
expect(handler.generateColorKey).toBe(mockedGenerateColorKey);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ describe('createEditorCore', () => {
const mockedGetDarkColor = 'DARK' as any;
const mockedTrustHtmlHandler = 'TRUST' as any;
const mockedDisposeErrorHandler = 'DISPOSE' as any;
const mockedGenerateColorKey = 'KEY' as any;
const mockedKnownColors = 'COLORS' as any;
const mockedOptions = {
coreApiOverride: {
a: 'b',
Expand All @@ -155,6 +157,8 @@ describe('createEditorCore', () => {
getDarkColor: mockedGetDarkColor,
trustedHTMLHandler: mockedTrustHtmlHandler,
disposeErrorHandler: mockedDisposeErrorHandler,
generateColorKey: mockedGenerateColorKey,
knownColors: mockedKnownColors,
} as any;

runTest(mockedDiv, mockedOptions, {
Expand Down Expand Up @@ -182,8 +186,8 @@ describe('createEditorCore', () => {
expect(DarkColorHandlerImpl.createDarkColorHandler).toHaveBeenCalledWith(
mockedDiv,
mockedGetDarkColor,
undefined,
undefined
mockedKnownColors,
mockedGenerateColorKey
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,49 @@ describe('setColor with darkColorHandler', () => {
darkModeColor: '--dark_green',
});
});

it('with customized generateColorKey', () => {
const generateColorKeySpy = jasmine
.createSpy('generateColorKey')
.and.callFake((color: string) => '--' + color + '_key');

const lightDiv = document.createElement('div');
const darkDiv = document.createElement('div');

darkColorHandler.generateColorKey = generateColorKeySpy;

setColor(lightDiv, 'red', true, false, darkColorHandler);
setColor(lightDiv, 'green', false, false, darkColorHandler);
setColor(darkDiv, 'red', true, true, darkColorHandler);
setColor(darkDiv, 'green', false, true, darkColorHandler);

expect(lightDiv.outerHTML).toBe('<div style="background-color: red; color: green;"></div>');
expect(darkDiv.outerHTML).toBe(
'<div style="background-color: var(--red_key, red); color: var(--green_key, green);"></div>'
);
expect(getDarkColorSpy).toHaveBeenCalledTimes(4);
expect(getDarkColorSpy).toHaveBeenCalledWith('green', undefined, 'text', lightDiv);
expect(getDarkColorSpy).toHaveBeenCalledWith('red', undefined, 'background', lightDiv);
expect(getDarkColorSpy).toHaveBeenCalledWith('green', undefined, 'text', darkDiv);
expect(getDarkColorSpy).toHaveBeenCalledWith('red', undefined, 'background', darkDiv);
expect(updateKnownColorSpy).toHaveBeenCalledTimes(4);
expect(updateKnownColorSpy).toHaveBeenCalledWith(false, '--red_key', {
lightModeColor: 'red',
darkModeColor: '--dark_red',
});
expect(updateKnownColorSpy).toHaveBeenCalledWith(false, '--green_key', {
lightModeColor: 'green',
darkModeColor: '--dark_green',
});
expect(updateKnownColorSpy).toHaveBeenCalledWith(true, '--red_key', {
lightModeColor: 'red',
darkModeColor: '--dark_red',
});
expect(updateKnownColorSpy).toHaveBeenCalledWith(true, '--green_key', {
lightModeColor: 'green',
darkModeColor: '--dark_green',
});
});
});

describe('parseColor', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ export interface ColorOptions {
/**
* A util function to generate color key for dark mode color.
* By default, the color key is generated from the light mode color. For example,
* color "#123456" will have the key "_123456", and
* color "rgb(0,0,0)" will have key "rgb_0_0_0_".
* color "#123456" will have the key "--darkColor__123456", and
* color "rgb(0,0,0)" will have key "--darkColor_rgb_0_0_0_".
* Pass in this function to customize this behavior.
* The return value must be a valid CSS variable, starts with "--"
*/
generateColorKey?: ColorTransformFunction;

Expand Down

0 comments on commit 1b67dd6

Please sign in to comment.