Skip to content

Commit

Permalink
test(plugin-layout): improve test
Browse files Browse the repository at this point in the history
  • Loading branch information
0x-leen committed Jul 8, 2021
1 parent 092c2ed commit 34858d2
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 10 deletions.
131 changes: 130 additions & 1 deletion packages/fower-plugin-layout/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { Parser } from '@fower/parser'
import { Atom } from '@fower/atom'
import plugin, { getFlexDirection } from '.'

const { isMatch, handleAtom } = plugin()
const { isMatch, handleAtom, beforeHandleAtom, afterAtomStyleCreate } = plugin()

const parser = new Parser({ row: true })

test('isMatch', () => {
expect(isMatch!('toCenter')).toEqual(true)
Expand All @@ -24,6 +26,33 @@ test('getFlexDirection', () => {
expect(getFlexDirection({})).toBe('row')
})

describe('beforeHandleAtom()', () => {
test('atom starts with row', () => {
const atom = handleAtom!(
new Atom({
propKey: 'toCenter',
propValue: true,
}),
parser,
)
beforeHandleAtom?.(atom, parser)
expect(atom.id).toEqual('row-toCenter')
})

test('not start with row', () => {
const atom = handleAtom!(
new Atom({
propKey: 'toCenter',
propValue: true,
}),
parser,
)
atom.id = 'row-' + atom.id
beforeHandleAtom?.(atom, parser)
expect(atom.id).toEqual('row-toCenter')
})
})

describe('row', () => {
const parser = new Parser({ row: true })
test('toCenter', () => {
Expand Down Expand Up @@ -281,3 +310,103 @@ describe('row or column props', () => {
expect(atom.handled).toEqual(true)
})
})

test('selfTop', () => {
const atom = handleAtom!(
new Atom({
propKey: 'selfTop',
propValue: true,
}),
parser,
)
expect(atom.style.alignSelf).toEqual('flex-start')
})

test('selfRight', () => {
const atom = handleAtom!(
new Atom({
propKey: 'selfRight',
propValue: true,
}),
parser,
)
expect(atom.style.alignSelf).toEqual('flex-end')
})

test('selfBottom', () => {
const atom = handleAtom!(
new Atom({
propKey: 'selfBottom',
propValue: true,
}),
parser,
)
expect(atom.style.alignSelf).toEqual('flex-end')
})

test('selfLeft', () => {
const atom = handleAtom!(
new Atom({
propKey: 'selfLeft',
propValue: true,
}),
parser,
)
expect(atom.style.alignSelf).toEqual('flex-start')
})

test('selfCenter', () => {
const atom = handleAtom!(
new Atom({
propKey: 'selfCenter',
propValue: true,
}),
parser,
)
expect(atom.style.alignSelf).toEqual('center')
})

test('selfAuto', () => {
const atom = handleAtom!(
new Atom({
propKey: 'selfAuto',
propValue: true,
}),
parser,
)
expect(atom.style.alignSelf).toEqual('auto')
})

describe('afterAtomStyleCreate', () => {
test('no atoms', () => {
parser.atoms = []
afterAtomStyleCreate?.(parser)
expect(parser.atoms.length).toEqual(0)
})

test('no matched atoms', () => {
const atom = handleAtom!(
new Atom({
propKey: 'foo',
propValue: true,
}),
parser,
)
parser.atoms = [atom]
afterAtomStyleCreate?.(parser)
expect(parser.atoms.length).toEqual(1)
})

test('no matched atoms', () => {
const atom = handleAtom!(
new Atom({
propKey: 'toCenter',
propValue: true,
}),
parser,
)
parser.atoms = [atom]
afterAtomStyleCreate?.(parser)
expect(parser.atoms.length).toEqual(3)
})
})
1 change: 0 additions & 1 deletion packages/fower-plugin-layout/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ export default (): FowerPlugin => {
isMatch,
beforeHandleAtom(atom, parser) {
const direction = getFlexDirection(parser.props)
// TODO:
if (atom.id.startsWith('row-') || atom.id.startsWith('column-')) {
return atom
}
Expand Down
8 changes: 0 additions & 8 deletions packages/parser/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,14 +376,6 @@ export class Parser {
* @param atom
*/
mutateAtom(atom: Atom): void {
for (const plugin of this.plugins) {
if (!plugin.isMatch?.(atom.key)) continue

if (plugin.beforeHandleAtom) {
atom = plugin.beforeHandleAtom(atom, this as any)
}
}

const cachedAtom = store.atomCache.get(atom.id)

if (cachedAtom) {
Expand Down

0 comments on commit 34858d2

Please sign in to comment.