Skip to content

Commit

Permalink
test: setTokens
Browse files Browse the repository at this point in the history
  • Loading branch information
Nnigmat committed Sep 2, 2021
1 parent 838de23 commit f9a0f9b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
21 changes: 21 additions & 0 deletions packages/core/src/internal/__tests__/fixtures/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,24 @@ export const simple = {
} as CompileOptions,
context: context.value,
}

export const anotherSimple = {
options: {
tokens: [
{ tokenAnother1: { value: 'value-another-1' } },
{ tokenAnother2: { value: 'value-another-2' } },
],
output: {
css: {
transforms: ['transform'],
files: [
{
destination: 'tokens.css',
format: 'format',
},
],
},
},
} as CompileOptions,
context: context.value,
}
42 changes: 41 additions & 1 deletion packages/core/src/internal/__tests__/observer.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createCompiler } from '../../compiler'
import { ThemekitObserver } from '../observer'
import { simple } from './fixtures/simple'
import { simple, anotherSimple } from './fixtures/simple'

describe('ThemekitObserver', () => {
test('should trigger watch after first compile', async () => {
Expand Down Expand Up @@ -56,6 +56,46 @@ describe('ThemekitObserver', () => {
],
})
})

test('should set new tokens on setTokens', async () => {
const onWatch = jest.fn()
const compile = createCompiler(simple.context)
const observer = new ThemekitObserver(simple.options, compile)
observer.watch(onWatch)
observer.update('token1', 'value-1-updated')
observer.setTokens(anotherSimple.options.tokens)
await wait(100)
expect(onWatch).toBeCalledTimes(3)
expect(onWatch).toHaveBeenLastCalledWith({
css: [
{
content: 'token-another1:value-another-1,token-another2:value-another-2',
destination: 'tokens.css',
},
],
})
})

test('should reset to tokens set on setTokens', async () => {
const onWatch = jest.fn()
const compile = createCompiler(simple.context)
const observer = new ThemekitObserver(simple.options, compile)
observer.watch(onWatch)
observer.update('token1', 'value-1-updated')
observer.setTokens(anotherSimple.options.tokens)
observer.update('token-another2', 'value-another-2-updated')
observer.reset()
await wait(100)
expect(onWatch).toBeCalledTimes(5)
expect(onWatch).toHaveBeenLastCalledWith({
css: [
{
content: 'token-another1:value-another-1,token-another2:value-another-2',
destination: 'tokens.css',
},
],
})
})
})

function wait(delay: number) {
Expand Down

0 comments on commit f9a0f9b

Please sign in to comment.