Skip to content

Commit

Permalink
Increase coverage for the new changes
Browse files Browse the repository at this point in the history
  • Loading branch information
elchininet committed Nov 27, 2023
1 parent 19b7255 commit 749df08
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 25 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
- Improve `getHarmony` method overloads to make it possible to omit optional parameters
- Improve types for `Mix` and `Harmony` making it possible to send also strings
- Make optional the `shades` and `tints` parameters of the `getShades` and `getTints` methods respectively
- Fix a bug in the `getBlendXXX` which was ignoring the `decimals` sent and always suing `MAX_DECIMALS`
- Fix a bug in the `getBlendXXX` and `toXXX` methods which were ignoring the `decimals` sent and always using `MAX_DECIMALS`
- Fix `CIELabObject` not being exported with the library

## [4.0.0] - 2023-11-26
Expand Down
19 changes: 9 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ import {
Mix,
MixString,
DEFAULT_BLEND_STEPS,
DEFAULT_SHADES_TINTS_STEPS,
MAX_DECIMALS
DEFAULT_SHADES_TINTS_STEPS
} from '#constants';
import {
rgbToHSL,
Expand Down Expand Up @@ -585,7 +584,7 @@ export class ColorTranslator {
const rgb = getColorReturn<RGBObject>(
color,
model,
MAX_DECIMALS,
options.decimals,
utils.translateColor.RGB
);
return CSS.RGB(rgb, detectedOptions);
Expand All @@ -607,7 +606,7 @@ export class ColorTranslator {
const rgba = getColorReturn<RGBObject>(
color,
model,
MAX_DECIMALS,
options.decimals,
utils.translateColor.RGBA
);
return CSS.RGB(rgba, detectedOptions);
Expand All @@ -629,7 +628,7 @@ export class ColorTranslator {
const hsl = getColorReturn<HSLObject>(
color,
model,
MAX_DECIMALS,
options.decimals,
utils.translateColor.HSL
);
return CSS.HSL(hsl, detectedOptions);
Expand All @@ -651,7 +650,7 @@ export class ColorTranslator {
const hsla = getColorReturn<HSLObject>(
color,
model,
MAX_DECIMALS,
options.decimals,
utils.translateColor.HSLA
);
return CSS.HSL(hsla, detectedOptions);
Expand All @@ -673,7 +672,7 @@ export class ColorTranslator {
const lab = getColorReturn<CIELabObject>(
color,
model,
MAX_DECIMALS,
options.decimals,
utils.translateColor.CIELab
);
return CSS.CIELab(lab, detectedOptions);
Expand All @@ -695,7 +694,7 @@ export class ColorTranslator {
const lab = getColorReturn<CIELabObject>(
color,
model,
MAX_DECIMALS,
options.decimals,
utils.translateColor.CIELabA
);
return CSS.CIELab(lab, detectedOptions);
Expand All @@ -717,7 +716,7 @@ export class ColorTranslator {
const cmyk = getColorReturn<CMYKObject>(
color,
model,
MAX_DECIMALS,
options.decimals,
utils.translateColor.CMYK
);
return CSS.CMYK(cmyk, detectedOptions);
Expand All @@ -739,7 +738,7 @@ export class ColorTranslator {
const cmyka = getColorReturn<CMYKObject>(
color,
model,
MAX_DECIMALS,
options.decimals,
utils.translateColor.CMYKA
);
return CSS.CMYK(cmyka, detectedOptions);
Expand Down
59 changes: 59 additions & 0 deletions tests/__snapshots__/blending.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,65 @@ exports[`ColorTranslator blending tests Blending with L*a*b colors 4`] = `
]
`;

exports[`ColorTranslator blending tests Blending with L*a*b colors 5`] = `
[
"lab(54.291734 80.812455 69.88504)",
"lab(29.563244 55.959822 -36.18907)",
"lab(29.567573 68.298653 -112.02943)",
]
`;

exports[`ColorTranslator blending tests Blending with L*a*b colors 6`] = `
[
"lab(54.291734 80.812455 69.88504 / 1)",
"lab(29.563244 55.959822 -36.18907 / 1)",
"lab(29.567573 68.298653 -112.02943 / 1)",
]
`;

exports[`ColorTranslator blending tests Blending with L*a*b colors 7`] = `
[
{
"L": 54.291734,
"a": 80.812455,
"b": 69.88504,
},
{
"L": 29.563244,
"a": 55.959822,
"b": -36.18907,
},
{
"L": 29.567573,
"a": 68.298653,
"b": -112.02943,
},
]
`;

exports[`ColorTranslator blending tests Blending with L*a*b colors 8`] = `
[
{
"A": 1,
"L": 54.291734,
"a": 80.812455,
"b": 69.88504,
},
{
"A": 1,
"L": 29.563244,
"a": 55.959822,
"b": -36.18907,
},
{
"A": 1,
"L": 29.567573,
"a": 68.298653,
"b": -112.02943,
},
]
`;

exports[`ColorTranslator blending tests Blending with decimals snapshots 1`] = `
[
"rgb(255 0 0)",
Expand Down
8 changes: 4 additions & 4 deletions tests/__snapshots__/shades-tints.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ColorTranslator shades and tints tests Shades snapshots with L*a*b functions from "lab(54.291734 80.812455 69.88504 / 1)" 1`] = `
exports[`ColorTranslator shades and tints tests Shades snapshots with L*a*b functions from "lab(54.2917337686 80.812455318 69.8850403235 / 1)" 1`] = `
[
"lab(45 70 61 / 1)",
"lab(36 60 51 / 1)",
Expand All @@ -10,7 +10,7 @@ exports[`ColorTranslator shades and tints tests Shades snapshots with L*a*b func
]
`;

exports[`ColorTranslator shades and tints tests Shades snapshots with L*a*b functions from "lab(54.291734 80.812455 69.88504)" 1`] = `
exports[`ColorTranslator shades and tints tests Shades snapshots with L*a*b functions from "lab(54.2917337686 80.812455318 69.8850403235)" 1`] = `
[
"lab(45 70 61)",
"lab(36 60 51)",
Expand Down Expand Up @@ -255,7 +255,7 @@ exports[`ColorTranslator shades and tints tests Shades tests from {"R":255,"G":0
]
`;

exports[`ColorTranslator shades and tints tests Tints snapshots with L*a*b functions from "lab(54.291734 80.812455 69.88504 / 1)" 1`] = `
exports[`ColorTranslator shades and tints tests Tints snapshots with L*a*b functions from "lab(54.2917337686 80.812455318 69.8850403235 / 1)" 1`] = `
[
"lab(56 76 56 / 1)",
"lab(61 65 38 / 1)",
Expand All @@ -265,7 +265,7 @@ exports[`ColorTranslator shades and tints tests Tints snapshots with L*a*b funct
]
`;

exports[`ColorTranslator shades and tints tests Tints snapshots with L*a*b functions from "lab(54.291734 80.812455 69.88504)" 1`] = `
exports[`ColorTranslator shades and tints tests Tints snapshots with L*a*b functions from "lab(54.2917337686 80.812455318 69.8850403235)" 1`] = `
[
"lab(56 76 56)",
"lab(61 65 38)",
Expand Down
24 changes: 24 additions & 0 deletions tests/blending.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,26 @@ describe('ColorTranslator blending tests', (): void => {
expect(obj.blendFn(from, to, r2.length, options)).toMatchObject(r2);

expect(obj.blendFn(from, to).length).toBe(5);
expect(
obj.blendFn(from, to, { decimals: 6 })
).toMatchObject(
obj.blendFn(from, to)
);
expect(
obj.blendFn(from, to, 4, { decimals: 6 })
).toMatchObject(
obj.blendFn(from, to, 4)
);
expect(
obj.blendFn(from, to, { cmykUnit: 'percent' })
).toMatchObject(
obj.blendFn(from, to)
);
expect(
obj.blendFn(from, to, 3, { cmykUnit: 'percent' })
).toMatchObject(
obj.blendFn(from, to, 3)
);

});

Expand All @@ -76,6 +96,10 @@ describe('ColorTranslator blending tests', (): void => {
blendLabFunctions.forEach((fn) => {
expect(fn(from, to)).toMatchSnapshot();
});

blendLabFunctions.forEach((fn) => {
expect(fn(from, to, 3)).toMatchSnapshot();
});
});

});
5 changes: 5 additions & 0 deletions tests/color-mixing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ describe('Color mixing with L*a*b colors', (): void => {
);
});
it('Return a mix in lab color with decimals', (): void => {
expect(
ColorTranslator.getMixCIELab(['#F00', '#00F'], Mix.ADDITIVE)
).toBe(
'lab(60.169696 93.550025 -60.498556)'
);
expect(
ColorTranslator.getMixCIELab(['#F00', '#00F'])
).toBe(
Expand Down
26 changes: 18 additions & 8 deletions tests/shades-tints.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,30 @@ describe('ColorTranslator shades and tints tests', (): void => {
const shades = ColorTranslator.getShades(input, 5);
expect(shades).toMatchObject(output);
});
it(`Shades tests without number of shades is equal to 5 from ${JSON.stringify(input)}`, (): void => {
const shades = ColorTranslator.getShades(input, 5);
const shadesDefault = ColorTranslator.getShades(input);
expect(shades).toMatchObject(shadesDefault);
});
it(`Shades tests with alpha from ${JSON.stringify(inputWithAlpha)}`, (): void => {
const output = shades_results.map((color: string) => fn(color + alpha));
const shades = ColorTranslator.getShades(inputWithAlpha, 5);
expect(shades).toMatchObject(output);
});
it(`Tints tests from ${JSON.stringify(input)}`, (): void => {
const output = tints_results.map((color: string) => fn(color));
const shades = ColorTranslator.getTints(input, 5);
expect(shades).toMatchObject(output);
const tints = ColorTranslator.getTints(input, 5);
expect(tints).toMatchObject(output);
});
it(`Tints tests without number of tints from ${JSON.stringify(input)}`, (): void => {
const tints = ColorTranslator.getTints(input, 5);
const tintsDefault = ColorTranslator.getTints(input);
expect(tints).toMatchObject(tintsDefault);
});
it(`Tints tests with alpha from ${JSON.stringify(inputWithAlpha)}`, (): void => {
const output = tints_results.map((color: string) => fn(color + alpha));
const shades = ColorTranslator.getTints(inputWithAlpha, 5);
expect(shades).toMatchObject(output);
const tints = ColorTranslator.getTints(inputWithAlpha, 5);
expect(tints).toMatchObject(output);
});
});

Expand All @@ -75,13 +85,13 @@ describe('ColorTranslator shades and tints tests', (): void => {
});
it(`Tints tests from ${JSON.stringify(input)}`, (): void => {
const output = tints_results.map((color: string) => fn(color, options));
const shades = ColorTranslator.getTints(input, 5, options);
expect(shades).toMatchObject(output);
const tints = ColorTranslator.getTints(input, 5, options);
expect(tints).toMatchObject(output);
});
it(`Tints tests with alpha from ${JSON.stringify(inputWithAlpha)}`, (): void => {
const output = tints_results.map((color: string) => fn(color + alpha, options));
const shades = ColorTranslator.getTints(inputWithAlpha, 5, options);
expect(shades).toMatchObject(output);
const tints = ColorTranslator.getTints(inputWithAlpha, 5, options);
expect(tints).toMatchObject(output);
});
it(`Shades tests from ${JSON.stringify(input)} with decimals`, (): void => {
const shades = ColorTranslator.getShades(input, 5, options);
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"compilerOptions": {
"outDir": "./dist/",
"module": "esnext",
"target": "es5",
"lib": ["ES2017"],
"target": "ES2020",
"lib": ["ES2020"],
"moduleResolution": "node",
"esModuleInterop": true,
"declaration": true,
Expand Down

0 comments on commit 749df08

Please sign in to comment.