Skip to content

Commit

Permalink
feat: remove jq
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomas Caraccia committed Feb 24, 2024
1 parent a0228db commit 49ddaee
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
16.15.0
20.9.0
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nodejs 14.17.1
nodejs 20.9.0
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@g2crowd/widget",
"version": "2.0.0",
"description": "Rails-friendly jquery plugin wrapper",
"description": "Rails-friendly plugin wrapper",
"repository": "https://github.com/g2crowd/widget",
"author": "Michael Wheeler",
"license": "MIT",
Expand All @@ -21,9 +21,6 @@
"jest": "^27.0.5",
"prettier": "^2.3.0"
},
"peerDependencies": {
"jquery": ">=1.6.2"
},
"module": "src/index.js",
"main": "src/index.js",
"files": [
Expand Down
6 changes: 3 additions & 3 deletions src/initWidgets.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import queue from './queue';
import { extractOptions } from '@g2crowd/extract-options';
import camelize from './camelize';
import { strategies } from './strategies';
import * as strategies from './strategies';

function simpleCaste(value) {
try {
Expand Down Expand Up @@ -50,10 +50,10 @@ const loadWidget = function (element, name, widgetQueue, registered) {

if (!existingPlugin) {
widgetQueue.add(() => {
strategies.get(pluginFn.init)(wrapped, element);
const initStrategy = strategies[pluginFn.init] || strategies.fallback(pluginFn.init, strategies.nextTick);
initStrategy(wrapped, element);
});
widgetQueue.flush();

element.dataset[`vvidget_${camelize(name)}`] = true;
}
};
Expand Down
13 changes: 0 additions & 13 deletions src/initiationStrategies.js

This file was deleted.

31 changes: 16 additions & 15 deletions src/strategies.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import initiationStrategies from './initiationStrategies';

export const strategies = initiationStrategies({
nextTick(pluginFn, $$) {
return window.setTimeout(() => pluginFn(), 0);
},

immediate(pluginFn, $$) {
return pluginFn() || {};
},

hover(pluginFn, $$) {
return $$.one('mouseover', () => pluginFn());
}
});
export function nextTick(pluginFn, _el) {
return window.setTimeout(() => pluginFn(), 0);
}
export function immediate(pluginFn, _el) {
return pluginFn() || {};
}
export function hover(pluginFn, el) {
const isJqueryEl = el.jquery !== undefined;
return isJqueryEl ?
el.one('mouseover', pluginFn) :
el.addEventListener('mouseover', pluginFn, { once: true });
}
export function fallback(strategy, fallback) {
console.warn(`Strategy ${strategy} not found, falling back to ${fallback.name} strategy.`);
return fallback;
}
19 changes: 0 additions & 19 deletions test/initiationStrategies.test.js

This file was deleted.

56 changes: 56 additions & 0 deletions test/strategies.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

import * as strategies from '../src/strategies';
jest.useFakeTimers();

describe('strategies', () => {
beforeEach(() => {
jest.clearAllTimers();
});
describe('fallback', () => {
it('should fall back to the provided strategy', () => {
const fallback = strategies.fallback('nonexistent', strategies.nextTick);
expect(fallback).toEqual(strategies.nextTick);
});

it('should log a warning', () => {
const nonexistentStrategy = 'nonexistent';
const fallbackStrategy = strategies.nextTick;
const warnSpy = jest.spyOn(console, 'warn').mockImplementation();
strategies.fallback(nonexistentStrategy, fallbackStrategy);
expect(warnSpy).toHaveBeenCalledWith(`Strategy ${nonexistentStrategy} not found, falling back to ${fallbackStrategy.name} strategy.`);
});
})
describe('nextTick', () => {
it('should call the plugin function', () => {
const pluginFn = jest.fn();
strategies.nextTick(pluginFn);
jest.runAllTimers();
expect(pluginFn).toHaveBeenCalled();
});
});
describe('immediate', () => {
it('should call the plugin function', () => {
const pluginFn = jest.fn();
strategies.immediate(pluginFn);
expect(pluginFn).toHaveBeenCalled();
});
});
describe('hover', () => {
describe('with a jQuery element', () => {
it('should call the plugin function', () => {
const pluginFn = jest.fn();
const el = { jquery: true, one: jest.fn() };
strategies.hover(pluginFn, el);
expect(el.one).toHaveBeenCalledWith('mouseover', pluginFn);
});
});
describe('with a DOM element', () => {
it('should call the plugin function', () => {
const pluginFn = jest.fn();
const el = { addEventListener: jest.fn() };
strategies.hover(pluginFn, el);
expect(el.addEventListener).toHaveBeenCalledWith('mouseover', pluginFn, { once: true });
});
})
});
})

0 comments on commit 49ddaee

Please sign in to comment.