Skip to content
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

Add JS API tests for calculations #1918

Merged
merged 23 commits into from Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
141 changes: 141 additions & 0 deletions js-api-spec/function.test.ts
Expand Up @@ -8,6 +8,11 @@ import {
compileString,
compileStringAsync,
sassNull,
sassTrue,
sassFalse,
SassNumber,
SassCalculation,
CalculationOperation,
} from 'sass';

import {spy} from './utils';
Expand Down Expand Up @@ -77,6 +82,129 @@ it('passes a default argument value', () => {
expect(fn).toHaveBeenCalled();
});

describe('simplifies', () => {
jerivas marked this conversation as resolved.
Show resolved Hide resolved
it('operations', () => {
const fn = spy(
() =>
new CalculationOperation(
jerivas marked this conversation as resolved.
Show resolved Hide resolved
'+',
new CalculationOperation('-', new SassNumber(10), new SassNumber(8)),
new CalculationOperation('*', new SassNumber(2), new SassNumber(3))
)
);

expect(
compileString('a {b: foo()}', {
functions: {'foo()': fn},
}).css
).toBe('a {\n b: 8;\n}');
});

it('calc()', () => {
const fn = spy(() =>
SassCalculation.calc(
new CalculationOperation('+', new SassNumber(1), new SassNumber(2))
)
);

expect(
compileString('a {b: foo()}', {
functions: {'foo()': fn},
}).css
).toBe('a {\n b: 3;\n}');
});

it('clamp()', () => {
const fn = spy(() =>
SassCalculation.clamp(
new SassNumber(1),
new SassNumber(2),
new SassNumber(3)
)
);

expect(
compileString('a {b: foo()}', {
functions: {'foo()': fn},
}).css
).toBe('a {\n b: 2;\n}');
});

it('min()', () => {
const fn = spy(() =>
SassCalculation.min([new SassNumber(1), new SassNumber(2)])
);

expect(
compileString('a {b: foo()}', {
functions: {'foo()': fn},
}).css
).toBe('a {\n b: 1;\n}');
});

it('max()', () => {
const fn = spy(() =>
SassCalculation.max([new SassNumber(1), new SassNumber(2)])
);

expect(
compileString('a {b: foo()}', {
functions: {'foo()': fn},
}).css
).toBe('a {\n b: 2;\n}');
});

it('complex calculations', () => {
const fn = spy(() =>
SassCalculation.calc(
new CalculationOperation(
'+',
SassCalculation.min([new SassNumber(3), new SassNumber(4)]),
new CalculationOperation(
'*',
SassCalculation.max([new SassNumber(5), new SassNumber(6)]),
new CalculationOperation(
'-',
new SassNumber(3),
new CalculationOperation(
'/',
new SassNumber(4),
new SassNumber(5)
)
)
)
)
)
);

expect(
compileString('a {b: foo()}', {
functions: {'foo()': fn},
}).css
).toBe('a {\n b: 16.2;\n}');
});
});

const primitiveValues = [
new SassString('quoted', {quotes: true}),
new SassString('unquoted', {quotes: false}),
new SassNumber(1),
sassTrue,
sassFalse,
];
describe('does not simplify', () => {
for (const value of primitiveValues) {
it(value.toString(), () => {
const fn = spy(() => value);
expect(
compileString('a {b: foo()}', {
functions: {'foo()': fn},
}).css
).toBe(`a {\n b: ${value.toString()};\n}`);
});
}
});

describe('gracefully handles a custom function', () => {
it('throwing', () => {
expect(() =>
Expand Down Expand Up @@ -158,6 +286,19 @@ describe('asynchronously', () => {
})
).toThrowSassException({line: 0});
});

it('simplifies', async () => {
const fn = spy(async () =>
SassCalculation.calc(
new CalculationOperation('+', new SassNumber(1), new SassNumber(2))
)
);

const result = await compileStringAsync('a {b: foo()}', {
functions: {'foo()': fn},
});
expect(result.css).toBe('a {\n b: 3;\n}');
});
});

describe('rejects a function signature that', () => {
Expand Down
1 change: 1 addition & 0 deletions js-api-spec/value/argument-list.test.ts
Expand Up @@ -97,6 +97,7 @@ describe('SassArgumentList', () => {

it("isn't any other type", () => {
expect(() => list.assertBoolean()).toThrow();
expect(() => list.assertCalculation()).toThrow();
expect(() => list.assertColor()).toThrow();
expect(() => list.assertFunction()).toThrow();
expect(() => list.assertMap()).toThrow();
Expand Down
2 changes: 2 additions & 0 deletions js-api-spec/value/boolean.test.ts
Expand Up @@ -25,6 +25,7 @@ describe('Sass boolean', () => {
});

it("isn't any other type", () => {
expect(value.assertCalculation).toThrow();
expect(value.assertColor).toThrow();
expect(value.assertFunction).toThrow();
expect(value.assertMap).toThrow();
Expand Down Expand Up @@ -54,6 +55,7 @@ describe('Sass boolean', () => {
});

it("isn't any other type", () => {
expect(value.assertCalculation).toThrow();
expect(value.assertColor).toThrow();
expect(value.assertFunction).toThrow();
expect(value.assertMap).toThrow();
Expand Down