mirror of
https://github.com/ueberdosis/tiptap.git
synced 2025-01-18 06:03:22 +08:00
add tests for mergeAttributes
This commit is contained in:
parent
d3319bea4e
commit
727442c3a5
@ -5,18 +5,19 @@ export default function mergeAttributes(...object: AnyObject[]) {
|
||||
const mergedAttributes = { ...items }
|
||||
|
||||
Object.entries(item).forEach(([key, value]) => {
|
||||
if (!mergedAttributes[key]) {
|
||||
const exists = mergedAttributes[key]
|
||||
|
||||
if (!exists) {
|
||||
mergedAttributes[key] = value
|
||||
return
|
||||
}
|
||||
|
||||
if (key === 'class') {
|
||||
mergedAttributes[key] = [mergedAttributes[key], value].join(' ')
|
||||
return
|
||||
}
|
||||
|
||||
if (key === 'style') {
|
||||
} else if (key === 'style') {
|
||||
mergedAttributes[key] = [mergedAttributes[key], value].join('; ')
|
||||
} else {
|
||||
mergedAttributes[key] = value
|
||||
}
|
||||
})
|
||||
|
||||
|
60
tests/cypress/integration/core/mergeAttributes.spec.ts
Normal file
60
tests/cypress/integration/core/mergeAttributes.spec.ts
Normal file
@ -0,0 +1,60 @@
|
||||
/// <reference types="cypress" />
|
||||
|
||||
import mergeAttributes from '@tiptap/core/src/utils/mergeAttributes'
|
||||
|
||||
describe('mergeAttributes', () => {
|
||||
it('should merge two objects', () => {
|
||||
const value = mergeAttributes({ id: 1 }, { class: 'foo' })
|
||||
|
||||
expect(value).to.deep.eq({
|
||||
id: 1,
|
||||
class: 'foo',
|
||||
})
|
||||
})
|
||||
|
||||
it('should merge multiple objects', () => {
|
||||
const value = mergeAttributes({ id: 1 }, { class: 'foo' }, { title: 'bar' })
|
||||
|
||||
expect(value).to.deep.eq({
|
||||
id: 1,
|
||||
class: 'foo',
|
||||
title: 'bar',
|
||||
})
|
||||
})
|
||||
|
||||
it('should overwrite values', () => {
|
||||
const value = mergeAttributes({ id: 1 }, { id: 2 })
|
||||
|
||||
expect(value).to.deep.eq({
|
||||
id: 2,
|
||||
})
|
||||
})
|
||||
|
||||
it('should merge classes', () => {
|
||||
const value = mergeAttributes({ class: 'foo' }, { class: 'bar' })
|
||||
|
||||
expect(value).to.deep.eq({
|
||||
class: 'foo bar',
|
||||
})
|
||||
})
|
||||
|
||||
it('should merge styles', () => {
|
||||
const value = mergeAttributes({ style: 'color: red' }, { style: 'background: green' })
|
||||
|
||||
expect(value).to.deep.eq({
|
||||
style: 'color: red; background: green',
|
||||
})
|
||||
})
|
||||
|
||||
it('should merge classes and styles', () => {
|
||||
const value = mergeAttributes(
|
||||
{ class: 'foo', style: 'color: red' },
|
||||
{ class: 'bar', style: 'background: green' },
|
||||
)
|
||||
|
||||
expect(value).to.deep.eq({
|
||||
class: 'foo bar',
|
||||
style: 'color: red; background: green',
|
||||
})
|
||||
})
|
||||
})
|
Loading…
Reference in New Issue
Block a user