aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/eval.spec.ts67
-rw-r--r--tests/parser.spec.ts127
2 files changed, 194 insertions, 0 deletions
diff --git a/tests/eval.spec.ts b/tests/eval.spec.ts
new file mode 100644
index 0000000..2212ba4
--- /dev/null
+++ b/tests/eval.spec.ts
@@ -0,0 +1,67 @@
+import { EvalActions, evalExpr } from '../src/eval'
+import { Expr } from '../src/parser'
+
+describe('eval', () => {
+ const deps: EvalActions = {
+ addClass: jest.fn(),
+ removeClass: jest.fn(),
+ delay: jest.fn(),
+ jsEval: jest.fn(),
+ loadCssx: jest.fn(),
+ getVariable: jest.fn(),
+ updateVariable: jest.fn(),
+ setAttribute: jest.fn(),
+ }
+
+ it('should add classes', async () => {
+ await evalExpr(
+ Expr.Call({
+ name: 'add-class',
+ args: [Expr.Identifier('element-id'), Expr.LiteralString('class-name')],
+ }),
+ deps,
+ )
+
+ expect(deps.addClass).toHaveBeenCalledTimes(1)
+ expect(deps.addClass).toHaveBeenCalledWith('element-id', 'class-name')
+ })
+
+ it('should remove classes', async () => {
+ await evalExpr(
+ Expr.Call({
+ name: 'remove-class',
+ args: [Expr.Identifier('element-id'), Expr.LiteralString('class-name')],
+ }),
+ deps,
+ )
+
+ expect(deps.removeClass).toHaveBeenCalledTimes(1)
+ expect(deps.removeClass).toHaveBeenCalledWith('element-id', 'class-name')
+ })
+
+ it('should add a delay', async () => {
+ await evalExpr(
+ Expr.Call({
+ name: 'delay',
+ args: [Expr.LiteralString('200')],
+ }),
+ deps,
+ )
+
+ expect(deps.delay).toHaveBeenCalledTimes(1)
+ expect(deps.delay).toHaveBeenCalledWith(200)
+ })
+
+ it('should get variable', async () => {
+ await evalExpr(
+ Expr.Call({
+ name: 'var',
+ args: [Expr.LiteralString('--my-var'), Expr.LiteralString('def value')],
+ }),
+ deps,
+ )
+
+ expect(deps.getVariable).toHaveBeenCalledTimes(1)
+ expect(deps.getVariable).toHaveBeenCalledWith('--my-var')
+ })
+})
diff --git a/tests/parser.spec.ts b/tests/parser.spec.ts
new file mode 100644
index 0000000..41fafa3
--- /dev/null
+++ b/tests/parser.spec.ts
@@ -0,0 +1,127 @@
+import { Expr, parse } from '../src/parser'
+
+describe('parser', () => {
+ it('should parse function call', () => {
+ expect(parse('hello()')).toEqual([Expr.Call({ name: 'hello', args: [] })])
+ expect(parse('hello ( wow , foo ) ')).toEqual([
+ Expr.Call({
+ name: 'hello',
+ args: [Expr.Identifier('wow'), Expr.Identifier('foo')],
+ }),
+ ])
+ expect(parse('hello(wow,foo)')).toEqual([
+ Expr.Call({
+ name: 'hello',
+ args: [Expr.Identifier('wow'), Expr.Identifier('foo')],
+ }),
+ ])
+ expect(parse('hello(wow,foo, coolio)')).toEqual([
+ Expr.Call({
+ name: 'hello',
+ args: [
+ Expr.Identifier('wow'),
+ Expr.Identifier('foo'),
+ Expr.Identifier('coolio'),
+ ],
+ }),
+ ])
+ expect(parse('hello(wow)')).toEqual([
+ Expr.Call({ name: 'hello', args: [Expr.Identifier('wow')] }),
+ ])
+ })
+
+ it('should parse sequential function calls', () => {
+ expect(parse('hello(world) foo-doo(bar, baz)')).toEqual([
+ Expr.Call({
+ name: 'hello',
+ args: [Expr.Identifier('world')],
+ }),
+ Expr.Call({
+ name: 'foo-doo',
+ args: [Expr.Identifier('bar'), Expr.Identifier('baz')],
+ }),
+ ])
+ })
+
+ it('should parse string literal', () => {
+ expect(parse(`"hello world toodles \' nice single quote there"`)).toEqual([
+ Expr.LiteralString(`hello world toodles \' nice single quote there`),
+ ])
+
+ expect(parse(` 'hello world toodles \" nice double quote there' `)).toEqual(
+ [Expr.LiteralString(`hello world toodles \" nice double quote there`)],
+ )
+ })
+
+ it('should parse var identifiers', () => {
+ expect(parse(`var(--hello, 'default')`)).toEqual([
+ Expr.Call({
+ name: 'var',
+ args: [Expr.VarIdentifier('--hello'), Expr.LiteralString(`default`)],
+ }),
+ ])
+
+ expect(parse(`calc(var(--hello))`)).toEqual([
+ Expr.Call({
+ name: 'calc',
+ args: [
+ Expr.Call({
+ name: 'var',
+ args: [Expr.VarIdentifier('--hello')],
+ }),
+ ],
+ }),
+ ])
+
+ expect(parse(`update(state-container, --count, var(--count))`)).toEqual([
+ Expr.Call({
+ name: 'update',
+ args: [
+ Expr.Identifier('state-container'),
+ Expr.VarIdentifier('--count'),
+ Expr.Call({
+ name: 'var',
+ args: [Expr.VarIdentifier('--count')],
+ }),
+ ],
+ }),
+ ])
+ })
+
+ it('should parse number and css units', () => {
+ expect(parse(`100`)).toEqual([Expr.LiteralNumber({ value: 100, unit: '' })])
+ expect(parse(`100s`)).toEqual([
+ Expr.LiteralNumber({ value: 100, unit: 's' }),
+ ])
+ expect(parse(`100ms`)).toEqual([
+ Expr.LiteralNumber({ value: 100, unit: 'ms' }),
+ ])
+ expect(parse(`3.82`)).toEqual([
+ Expr.LiteralNumber({ value: 3.82, unit: '' }),
+ ])
+ expect(parse(`3.82s`)).toEqual([
+ Expr.LiteralNumber({ value: 3.82, unit: 's' }),
+ ])
+ expect(parse(`3.82ms`)).toEqual([
+ Expr.LiteralNumber({ value: 3.82, unit: 'ms' }),
+ ])
+ expect(parse(`-100`)).toEqual([
+ Expr.LiteralNumber({ value: -100, unit: '' }),
+ ])
+ expect(parse(`-100s`)).toEqual([
+ Expr.LiteralNumber({ value: -100, unit: 's' }),
+ ])
+ expect(parse(`-100ms`)).toEqual([
+ Expr.LiteralNumber({ value: -100, unit: 'ms' }),
+ ])
+ expect(parse(`-3.82`)).toEqual([
+ Expr.LiteralNumber({ value: -3.82, unit: '' }),
+ ])
+ expect(parse(`-3.82s`)).toEqual([
+ Expr.LiteralNumber({ value: -3.82, unit: 's' }),
+ ])
+ expect(parse(`-3.82ms`)).toEqual([
+ Expr.LiteralNumber({ value: -3.82, unit: 'ms' }),
+ ])
+ })
+})