-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3048239
commit 3a0bd29
Showing
10 changed files
with
194 additions
and
21 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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -859,6 +859,7 @@ export class ColorTranslator { | |
} | ||
|
||
export { | ||
InputOptions, | ||
Harmony, | ||
Mix, | ||
HEXObject, | ||
|
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,123 @@ | ||
import { ColorTranslator, InputOptions } from '../src'; | ||
import { Options } from '../src/@types'; | ||
import { DEFAULT_OPTIONS } from '../src/constants'; | ||
|
||
interface TestCase { | ||
options: InputOptions; | ||
extraOptions?: InputOptions; | ||
rgb: string; | ||
rgba: string; | ||
hsl: string; | ||
hsla: string; | ||
isDefault: boolean; | ||
} | ||
|
||
describe('ColorTranslator CSS config options', () => { | ||
|
||
const COLOR = '#FF00FF'; | ||
|
||
const TEST_CASES: TestCase[] = [ | ||
{ | ||
options: { legacyCSS: true }, | ||
rgb: 'rgb(255,0,255)', | ||
rgba: 'rgba(255,0,255,1)', | ||
hsl: 'hsl(300,100%,50%)', | ||
hsla: 'hsla(300,100%,50%,1)', | ||
isDefault: false | ||
}, | ||
{ | ||
options: { legacyCSS: false }, | ||
rgb: 'rgb(255 0 255)', | ||
rgba: 'rgb(255 0 255 / 1)', | ||
hsl: 'hsl(300 100% 50%)', | ||
hsla: 'hsl(300 100% 50% / 1)', | ||
isDefault: true | ||
}, | ||
{ | ||
options: { spacesAfterCommas: true }, | ||
extraOptions: { legacyCSS: true }, | ||
rgb: 'rgb(255, 0, 255)', | ||
rgba: 'rgba(255, 0, 255, 1)', | ||
hsl: 'hsl(300, 100%, 50%)', | ||
hsla: 'hsla(300, 100%, 50%, 1)', | ||
isDefault: false | ||
}, | ||
{ | ||
options: { spacesAfterCommas: false }, | ||
extraOptions: { legacyCSS: true }, | ||
rgb: 'rgb(255,0,255)', | ||
rgba: 'rgba(255,0,255,1)', | ||
hsl: 'hsl(300,100%,50%)', | ||
hsla: 'hsla(300,100%,50%,1)', | ||
isDefault: true | ||
} | ||
]; | ||
|
||
TEST_CASES.forEach((testCase: TestCase): void => { | ||
|
||
const { | ||
options, | ||
extraOptions = {}, | ||
rgb, | ||
rgba, | ||
hsl, | ||
hsla, | ||
isDefault | ||
} = testCase; | ||
|
||
it(`Check ${JSON.stringify(options)}`, () => { | ||
const mergedOptions = { ...options, ...extraOptions }; | ||
const instance = new ColorTranslator(COLOR, mergedOptions); | ||
expect(instance.RGB).toBe(rgb); | ||
expect(instance.RGBA).toBe(rgba); | ||
expect(instance.HSL).toBe(hsl); | ||
expect(instance.HSLA).toBe(hsla); | ||
expect(ColorTranslator.toRGB(COLOR, mergedOptions)).toBe(rgb); | ||
expect(ColorTranslator.toRGBA(COLOR, mergedOptions)).toBe(rgba); | ||
expect(ColorTranslator.toHSL(COLOR, mergedOptions)).toBe(hsl); | ||
expect(ColorTranslator.toHSLA(COLOR, mergedOptions)).toBe(hsla); | ||
}); | ||
|
||
if (isDefault) { | ||
|
||
const key: keyof Options = Object.keys(options)[0] as keyof Options; | ||
const { [key]: defaultProp, ...defaultOptions } = { ...DEFAULT_OPTIONS }; | ||
|
||
it(`Check default value of ${key}:${defaultProp}`, () => { | ||
const mergedOptions = { ...defaultOptions, ...extraOptions }; | ||
const instance = new ColorTranslator(COLOR, mergedOptions); | ||
expect(instance.RGB).toBe(rgb); | ||
expect(instance.RGBA).toBe(rgba); | ||
expect(instance.HSL).toBe(hsl); | ||
expect(instance.HSLA).toBe(hsla); | ||
expect(ColorTranslator.toRGB(COLOR, mergedOptions)).toBe(rgb); | ||
expect(ColorTranslator.toRGBA(COLOR, mergedOptions)).toBe(rgba); | ||
expect(ColorTranslator.toHSL(COLOR, mergedOptions)).toBe(hsl); | ||
expect(ColorTranslator.toHSLA(COLOR, mergedOptions)).toBe(hsla); | ||
}); | ||
|
||
} | ||
|
||
}); | ||
|
||
}); | ||
|
||
describe('ColorTranslator CSS config options autodetection', () => { | ||
|
||
it(`legacyCSS auto detection`, () => { | ||
|
||
expect(ColorTranslator.toRGB('rgba(255,0,255)')).toBe('rgb(255,0,255)'); | ||
expect(ColorTranslator.toHSLA('rgba(255 0 255 / 1)')).toBe('hsl(300 100% 50% / 1)'); | ||
|
||
}); | ||
|
||
it(`spacesAfterCommas auto detection`, () => { | ||
|
||
expect(ColorTranslator.toRGBA('rgba(255,0,255)')).toBe('rgba(255,0,255,1)'); | ||
expect(ColorTranslator.toRGBA('rgba(255, 0,255)')).toBe('rgba(255,0,255,1)'); | ||
expect(ColorTranslator.toHSL('rgba(255, 0, 255, 1)')).toBe('hsl(300, 100%, 50%)'); | ||
expect(ColorTranslator.toHSL('rgba(255,0,255, 1)')).toBe('hsl(300,100%,50%)'); | ||
|
||
}); | ||
|
||
}); |