Validating Plugin Extension System in Day.js
This test suite validates the plugin extension functionality in Day.js, focusing on how plugins can extend both instance and static methods. It verifies the core plugin architecture and utility access mechanisms.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
iamkun/dayjs
test/plugin.test.js
import MockDate from 'mockdate'
import dayjs from '../src'
const testPlugin = (o, c, d) => {
c.prototype.newApi = () => ('hello world')
d.newFunc = () => ('hi world')
}
const testPluginWithConfig = (o, c) => {
c.prototype.newApiWithConfig = () => (`hello world ${o || ''}`)
}
dayjs.extend(testPlugin)
dayjs.extend(testPluginWithConfig, 'good')
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
it('Plugin extend method and option', () => {
expect(dayjs().newApi()).toBe('hello world')
expect(dayjs().newApiWithConfig()).toBe('hello world good')
})
it('Plugin extend dayjs', () => {
expect(dayjs.newFunc()).toBe('hi world')
})
it('Plugin use core utils', () => {
// u => isUndefined
expect(dayjs().$utils().u).toBeInstanceOf(Function)
})