Skip to content

Commit

Permalink
Add JS API tests for calculations (#1918)
Browse files Browse the repository at this point in the history
Co-authored-by: Jonny Gerig Meyer <[email protected]>
  • Loading branch information
jerivas and jgerigmeyer authored Jul 19, 2023
1 parent 5b2a0fc commit e6e6762
Show file tree
Hide file tree
Showing 10 changed files with 487 additions and 14 deletions.
67 changes: 53 additions & 14 deletions js-api-spec/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
compileString,
compileStringAsync,
sassNull,
SassCalculation,
} from 'sass';

import {spy} from './utils';
Expand Down Expand Up @@ -98,14 +99,27 @@ describe('gracefully handles a custom function', () => {
).toThrowSassException({line: 0});
});

it('returning a non-Value', () => {
expect(() =>
compileString('a {b: foo()}', {
functions: {
'foo()': (() => 'wrong') as unknown as CustomFunction<'sync'>,
},
})
).toThrowSassException({line: 0});
describe('returning a non-Value', () => {
it('directly', () => {
expect(() =>
compileString('a {b: foo()}', {
functions: {
'foo()': (() => 'wrong') as unknown as CustomFunction<'sync'>,
},
})
).toThrowSassException({line: 0});
});

it('in a calculation', () => {
expect(() =>
compileString('a {b: foo()}', {
functions: {
'foo()': () =>
SassCalculation.calc('wrong' as unknown as SassString),
},
})
).toThrowSassException({line: 0});
});
});
});

Expand Down Expand Up @@ -151,12 +165,37 @@ describe('asynchronously', () => {
expect(fn).toHaveBeenCalled();
});

it('gracefully handles promise rejections', async () => {
await expectAsync(() =>
compileStringAsync('a {b: foo(bar)}', {
functions: {'foo($arg)': () => Promise.reject('heck')},
})
).toThrowSassException({line: 0});
describe('gracefully handles', () => {
it('promise rejections', async () => {
await expectAsync(() =>
compileStringAsync('a {b: foo(bar)}', {
functions: {'foo($arg)': () => Promise.reject('heck')},
})
).toThrowSassException({line: 0});
});

describe('returning a non-Value', () => {
it('directly', async () => {
await expectAsync(() =>
compileStringAsync('a {b: foo()}', {
functions: {
'foo()': (() => 'wrong') as unknown as CustomFunction<'async'>,
},
})
).toThrowSassException({line: 0});
});

it('in a calculation', async () => {
await expectAsync(() =>
compileStringAsync('a {b: foo()}', {
functions: {
'foo()': () =>
SassCalculation.calc('wrong' as unknown as SassString),
},
})
).toThrowSassException({line: 0});
});
});
});
});

Expand Down
1 change: 1 addition & 0 deletions js-api-spec/value/argument-list.test.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading

0 comments on commit e6e6762

Please sign in to comment.