Skip to content

Commit

Permalink
fix: preset tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JiLiZART committed Dec 26, 2023
1 parent 2e604c6 commit 583a6e6
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 57 deletions.
4 changes: 2 additions & 2 deletions packages/bbob-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export interface BBobCoreTagNodeTree extends Array<TagNode> {

export type BBobPlugins = BBobPluginFunction | BBobPluginFunction[]

function createTree<Options extends BBobCoreOptions = BBobCoreOptions>(tree: TagNode[], options: Options) {
export function createTree<Options extends BBobCoreOptions = BBobCoreOptions>(tree: TagNode[], options: Options) {
const extendedTree = tree as BBobCoreTagNodeTree

extendedTree.messages = [...(extendedTree.messages || [])]
Expand All @@ -58,7 +58,7 @@ function createTree<Options extends BBobCoreOptions = BBobCoreOptions>(tree: Tag
}

export default function bbob<InputValue = string | TagNode[], Options extends BBobCoreOptions = BBobCoreOptions>(
plugs: BBobPlugins
plugs?: BBobPlugins
): BBobCore<InputValue, Options> {
const plugins = typeof plugs === 'function' ? [plugs] : plugs || [];
const mockRender = () => ""
Expand Down
31 changes: 31 additions & 0 deletions packages/bbob-plugin-helper/test/TagNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,37 @@ describe('@bbob/plugin-helper/TagNode', () => {
expect(TagNode.isOf(tagNode, 'test')).toBe(true);
});

test('attr', () => {
const tagNode = TagNode.create('test', {test: 1}, ['Hello']);

tagNode.attr('foo', 'bar')

expect(tagNode.attrs.foo).toBe('bar');
});

test('append', () => {
const tagNode = TagNode.create('test', {test: 1}, ['Hello']);

tagNode.append('World')

expect(tagNode.content).toEqual(['Hello', 'World']);
});

test('length', () => {
const tagNode = TagNode.create('test', {test: 1}, ['Hello', 'World']);

expect(tagNode.length).toEqual('HelloWorld'.length);
});

test('toTagNode', () => {
const tagNode = TagNode.create('test', {test: 1}, ['Hello']);
const newTagNode = tagNode.toTagNode()

expect(newTagNode !== tagNode).toBe(true);
expect(newTagNode.tag).toEqual(tagNode.tag);
expect(newTagNode.content).toEqual(tagNode.content);
});

describe('toString', () => {
test('tag with content and params', () => {
const tagNode = TagNode.create('test', {test: 1}, ['Hello']);
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion packages/bbob-preset/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export type ProcessorFunction = typeof process
type ProcessorReturnType = ReturnType<ProcessorFunction>

export interface PresetExecutor<TagName extends string = string, AttrValue = unknown> extends BBobPluginFunction {
(tree: BBobCoreTagNodeTree, core: BbobPluginOptions): ProcessorReturnType
(tree: BBobCoreTagNodeTree, core?: BbobPluginOptions): ProcessorReturnType
options: PresetOptions,
}

Expand Down
54 changes: 0 additions & 54 deletions packages/bbob-preset/test/index.test.js

This file was deleted.

61 changes: 61 additions & 0 deletions packages/bbob-preset/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { createPreset, PresetTagsDefinition } from '../src';
import { BBobCoreOptions, createTree } from '@bbob/core'

describe('@bbob/preset', () => {
const presetFactory = (defTags: PresetTagsDefinition) => {
const processor = jest.fn((tags, tree, core, options) => tags)

return {
preset: createPreset(defTags, processor),
processor,
core: {} as BBobCoreOptions
}
}

test('create', () => {
const defTags = { test: () => 'test' }
const options = { foo: 'bar' }
const tree = createTree([], {})
const { preset, processor, core } = presetFactory(defTags);

expect(preset.extend)
.toBeDefined();
expect(preset)
.toBeInstanceOf(Function);

expect(preset(options)(tree)).toEqual(defTags);

expect(processor.mock.calls.length).toBe(1);
});
test('extend', () => {
const defTags = { foo: () => 'foo' }
const extendedTags = { bar: () => 'bar' }
const options = { foo: 'bar' }
const tree = createTree([], {})
const { preset, processor, core } = presetFactory(defTags);
const newPreset = preset.extend(tags => ({ ...tags, ...extendedTags }));

expect(preset)
.toBeInstanceOf(Function);
expect(newPreset)
.toBeInstanceOf(Function);

const newPresetRes = newPreset(options)

expect(newPresetRes(tree)).toEqual({...defTags, ...extendedTags});

expect(processor.mock.calls.length).toBe(1);
});
test('pass options', () => {
const { preset, processor } = presetFactory({ test: () => 'test' });
const newPreset = preset.extend((tags, options) => ({ bar: () => 'bar' }));

const instance = preset({ foo: 'bar' });
const instance2 = newPreset({ some: 'some' });

expect(instance.options)
.toEqual({ foo: 'bar' });
expect(instance2.options)
.toEqual({ some: 'some' })
});
});

0 comments on commit 583a6e6

Please sign in to comment.