diff --git a/.changeset/soft-melons-kiss.md b/.changeset/soft-melons-kiss.md new file mode 100644 index 0000000000..1317af7ac9 --- /dev/null +++ b/.changeset/soft-melons-kiss.md @@ -0,0 +1,5 @@ +--- +'singleton-manager': patch +--- + +add node/ssr support diff --git a/packages/singleton-manager/package.json b/packages/singleton-manager/package.json index 883cd91904..5b41152ba8 100644 --- a/packages/singleton-manager/package.json +++ b/packages/singleton-manager/package.json @@ -35,6 +35,7 @@ "start:singleton": "es-dev-server -c demo/singleton/server.js", "start:singleton-complex": "es-dev-server -c demo/singleton-complex/server.js", "test": "cd ../../ && npm run test:browser -- --group singleton-manager", + "test:node": "mocha test-node", "types": "wireit" }, "keywords": [ diff --git a/packages/singleton-manager/src/SingletonManagerClass.js b/packages/singleton-manager/src/SingletonManagerClass.js index 6a10af55f7..7cf4ae3350 100644 --- a/packages/singleton-manager/src/SingletonManagerClass.js +++ b/packages/singleton-manager/src/SingletonManagerClass.js @@ -1,9 +1,18 @@ const sym = Symbol.for('lion::SingletonManagerClassStorage'); +/** + * Allow compatibility with node-js (for ssr). + * In the future, we can just use globalThis directly + * (for now, we're backwards compatible with browsers that still only use window, since we don't know all contexts singleton-manager is used in). + */ +// eslint-disable-next-line no-undef +const globalThisOrWindow = globalThis || window; export class SingletonManagerClass { constructor() { /** @protected */ - this._map = window[sym] ? window[sym] : (window[sym] = new Map()); + this._map = globalThisOrWindow[sym] + ? globalThisOrWindow[sym] + : (globalThisOrWindow[sym] = new Map()); } /** diff --git a/packages/singleton-manager/test-node/SingletonManagerClass.test.js b/packages/singleton-manager/test-node/SingletonManagerClass.test.js new file mode 100644 index 0000000000..cbc83a1432 --- /dev/null +++ b/packages/singleton-manager/test-node/SingletonManagerClass.test.js @@ -0,0 +1,4 @@ +import { expect } from 'chai'; +import { runSingletonManagerClassSuite } from '../test-suites/runSingletonManagerClassSuite.suite.js'; + +runSingletonManagerClassSuite({ expect }); diff --git a/packages/singleton-manager/test-suites/runSingletonManagerClassSuite.suite.js b/packages/singleton-manager/test-suites/runSingletonManagerClassSuite.suite.js new file mode 100644 index 0000000000..c775576a43 --- /dev/null +++ b/packages/singleton-manager/test-suites/runSingletonManagerClassSuite.suite.js @@ -0,0 +1,39 @@ +import { SingletonManagerClass } from 'singleton-manager'; + +/** + * @param {{expect: function}} config + */ +export function runSingletonManagerClassSuite({ expect }) { + describe('SingletonManagerClass', () => { + it('returns undefined and has false if not set', async () => { + const mngr = new SingletonManagerClass(); + expect(mngr.get('overlays/overlays.js::0.13.x')).to.be.undefined; + expect(mngr.has('overlays/overlays.js::0.13.x')).to.be.false; + }); + + it('return value and has true if set', () => { + const mngr = new SingletonManagerClass(); + mngr.set('overlays/overlays.js::0.13.x', 'is-set'); + expect(mngr.get('overlays/overlays.js::0.13.x')).to.equal('is-set'); + expect(mngr.has('overlays/overlays.js::0.13.x')).to.be.true; + // make sure non set values are still correct + expect(mngr.get('overlays/overlays.js::0.14.x')).to.be.undefined; + expect(mngr.has('overlays/overlays.js::0.14.x')).to.be.false; + }); + + it('does not override existing keys (e.g. subsequential calls for the same keys are ignored)', () => { + const mngr = new SingletonManagerClass(); + mngr.set('overlays/overlays.js::0.14.x', 'is-set'); + mngr.set('overlays/overlays.js::0.14.x', 'new-set'); + expect(mngr.get('overlays/overlays.js::0.14.x')).to.equal('is-set'); + }); + + it('should return the same value with two SingletonManager instances', () => { + const mngr1 = new SingletonManagerClass(); + const mngr2 = new SingletonManagerClass(); + + mngr1.set('overlays/overlays.js::0.15.x', 'is-set'); + expect(mngr2.get('overlays/overlays.js::0.15.x')).to.equal('is-set'); + }); + }); +} diff --git a/packages/singleton-manager/test/SingletonManagerClass.test.js b/packages/singleton-manager/test/SingletonManagerClass.test.js index c2ca1b4ad0..46cae2ff25 100644 --- a/packages/singleton-manager/test/SingletonManagerClass.test.js +++ b/packages/singleton-manager/test/SingletonManagerClass.test.js @@ -1,36 +1,4 @@ import { expect } from '@open-wc/testing'; +import { runSingletonManagerClassSuite } from '../test-suites/runSingletonManagerClassSuite.suite.js'; -import { SingletonManagerClass } from 'singleton-manager'; - -describe('SingletonManagerClass', () => { - it('returns undefined and has false if not set', async () => { - const mngr = new SingletonManagerClass(); - expect(mngr.get('overlays/overlays.js::0.13.x')).to.be.undefined; - expect(mngr.has('overlays/overlays.js::0.13.x')).to.be.false; - }); - - it('return value and has true if set', () => { - const mngr = new SingletonManagerClass(); - mngr.set('overlays/overlays.js::0.13.x', 'is-set'); - expect(mngr.get('overlays/overlays.js::0.13.x')).to.equal('is-set'); - expect(mngr.has('overlays/overlays.js::0.13.x')).to.be.true; - // make sure non set values are still correct - expect(mngr.get('overlays/overlays.js::0.14.x')).to.be.undefined; - expect(mngr.has('overlays/overlays.js::0.14.x')).to.be.false; - }); - - it('does not override existing keys (e.g. subsequential calls for the same keys are ignored)', () => { - const mngr = new SingletonManagerClass(); - mngr.set('overlays/overlays.js::0.14.x', 'is-set'); - mngr.set('overlays/overlays.js::0.14.x', 'new-set'); - expect(mngr.get('overlays/overlays.js::0.14.x')).to.equal('is-set'); - }); - - it('should return the same value with two SingletonManager instances', () => { - const mngr1 = new SingletonManagerClass(); - const mngr2 = new SingletonManagerClass(); - - mngr1.set('overlays/overlays.js::0.15.x', 'is-set'); - expect(mngr2.get('overlays/overlays.js::0.15.x')).to.equal('is-set'); - }); -}); +runSingletonManagerClassSuite({ expect }); diff --git a/packages/singleton-manager/tsconfig.json b/packages/singleton-manager/tsconfig.json index e7787a4061..1e212b389b 100644 --- a/packages/singleton-manager/tsconfig.json +++ b/packages/singleton-manager/tsconfig.json @@ -2,8 +2,8 @@ "extends": "../../tsconfig.json", "compilerOptions": { "outDir": "./dist-types", - "rootDir": ".", + "rootDir": "." }, - "include": ["src", "test", "types"], + "include": ["src", "test", "test-node", "test-suites", "types"], "exclude": ["dist-types"] }