From 8fd4d19db7b3f94445a238954e8d917468676eca Mon Sep 17 00:00:00 2001 From: Jason Mitchell Date: Mon, 20 Apr 2020 12:02:35 -0700 Subject: [PATCH] fix: Legacy persisters and adapters should register (#325) --- packages/@pollyjs/adapter/src/index.js | 9 ++- .../@pollyjs/core/src/-private/container.js | 18 +---- packages/@pollyjs/core/src/polly.js | 10 +-- .../@pollyjs/core/tests/unit/polly-test.js | 68 +++++++++++++++++++ packages/@pollyjs/persister/src/index.js | 12 ++-- packages/@pollyjs/utils/src/index.js | 2 + .../utils/src/utils/get-factory-id.js | 11 +++ 7 files changed, 101 insertions(+), 29 deletions(-) create mode 100644 packages/@pollyjs/utils/src/utils/get-factory-id.js diff --git a/packages/@pollyjs/adapter/src/index.js b/packages/@pollyjs/adapter/src/index.js index 8a6167b3..683a912c 100644 --- a/packages/@pollyjs/adapter/src/index.js +++ b/packages/@pollyjs/adapter/src/index.js @@ -4,7 +4,8 @@ import { EXPIRY_STRATEGIES, PollyError, Serializers, - assert + assert, + getFactoryId } from '@pollyjs/utils'; import isExpired from './utils/is-expired'; @@ -35,7 +36,9 @@ export default class Adapter { get options() { return { ...(this.defaultOptions || {}), - ...((this.polly.config.adapterOptions || {})[this.constructor.id] || {}) + ...((this.polly.config.adapterOptions || {})[ + getFactoryId(this.constructor) + ] || {}) }; } @@ -222,7 +225,7 @@ export default class Adapter { assert(message, ...args) { assert( - `[${this.constructor.type}:${this.constructor.id}] ${message}`, + `[${this.constructor.type}:${getFactoryId(this.constructor)}] ${message}`, ...args ); } diff --git a/packages/@pollyjs/core/src/-private/container.js b/packages/@pollyjs/core/src/-private/container.js index d76ecd42..abe329f9 100644 --- a/packages/@pollyjs/core/src/-private/container.js +++ b/packages/@pollyjs/core/src/-private/container.js @@ -1,19 +1,7 @@ -import { assert } from '@pollyjs/utils'; - -export function idOfFactory(Factory) { - if (Factory.hasOwnProperty('id')) { - return Factory.id; - } - - console.warn( - `[Polly] ${Factory.id}:${Factory.name} "name" is deprecated and has been renamed to "id".` - ); - - return Factory.name; -} +import { assert, getFactoryId } from '@pollyjs/utils'; function keyFor(Factory) { - return `${Factory.type}:${idOfFactory(Factory)}`; + return `${Factory.type}:${getFactoryId(Factory)}`; } export class Container { @@ -33,7 +21,7 @@ export class Container { ); const { type } = Factory; - const name = idOfFactory(Factory); + const name = getFactoryId(Factory); assert( `Invalid registration id provided. Expected string, received: "${typeof name}"`, diff --git a/packages/@pollyjs/core/src/polly.js b/packages/@pollyjs/core/src/polly.js index 47d519ab..420ce44a 100644 --- a/packages/@pollyjs/core/src/polly.js +++ b/packages/@pollyjs/core/src/polly.js @@ -1,9 +1,9 @@ -import { MODES, assert } from '@pollyjs/utils'; +import { MODES, getFactoryId, assert } from '@pollyjs/utils'; import { version } from '../package.json'; import Logger from './-private/logger'; -import { Container, idOfFactory } from './-private/container'; +import { Container } from './-private/container'; import DefaultConfig from './defaults/config'; import PollyRequest from './-private/request'; import guidForRecording from './utils/guid-for-recording'; @@ -170,7 +170,7 @@ export default class Polly { if (persister) { if (typeof persister === 'function') { container.register(persister); - persister = idOfFactory(persister); + persister = getFactoryId(persister); } assert( @@ -262,7 +262,7 @@ export default class Polly { if (typeof idOrFactoryIdGetter === 'function') { container.register(idOrFactoryIdGetter); - adapterId = idOfFactory(idOrFactoryIdGetter); + adapterId = getFactoryId(idOrFactoryIdGetter); } assert( @@ -288,7 +288,7 @@ export default class Polly { let adapterId = idOrFactoryIdGetter; if (typeof idOrFactoryIdGetter === 'function') { - adapterId = idOfFactory(idOrFactoryIdGetter); + adapterId = getFactoryId(idOrFactoryIdGetter); } if (adapters.has(adapterId)) { diff --git a/packages/@pollyjs/core/tests/unit/polly-test.js b/packages/@pollyjs/core/tests/unit/polly-test.js index 6cfeb2bb..2903bc84 100644 --- a/packages/@pollyjs/core/tests/unit/polly-test.js +++ b/packages/@pollyjs/core/tests/unit/polly-test.js @@ -132,6 +132,43 @@ describe('Unit | Polly', function() { expect(persistCalled).to.be.true; }); + it('it supports legacy custom persisters', async function() { + const fakeContext = {}; + let instantiated, persistCalled, recognizedOptions; + + class MockPersister extends Persister { + static get name() { + return 'mock'; + } + + constructor() { + super(...arguments); + instantiated = true; + } + + persist() { + persistCalled = true; + recognizedOptions = this.options; + super.persist(...arguments); + } + } + + const polly = new Polly('recording name', { + persister: MockPersister, + persisterOptions: { + mock: { + context: fakeContext + } + } + }); + + await polly.stop(); + + expect(instantiated).to.be.true; + expect(persistCalled).to.be.true; + expect(recognizedOptions.context).to.equal(fakeContext); + }); + describe('configure', function() { setupPolly(); @@ -213,6 +250,37 @@ describe('Unit | Polly', function() { expect(connectCalled).to.be.true; }); + it('should connect legacy custom adapters', async function() { + const fakeContext = {}; + let connectCalled = false; + let options; + + class MockAdapter extends Adapter { + static get name() { + return 'mock'; + } + + onConnect() { + connectCalled = true; + options = this.options; + } + + onDisconnect() {} + } + + expect(connectCalled).to.be.false; + this.polly.configure({ + adapters: [MockAdapter], + adapterOptions: { + mock: { + context: fakeContext + } + } + }); + expect(connectCalled).to.be.true; + expect(options.context).to.equal(fakeContext); + }); + it('should disconnect from adapters before connecting', async function() { let connectCount = 0; let disconnectCount = 0; diff --git a/packages/@pollyjs/persister/src/index.js b/packages/@pollyjs/persister/src/index.js index 4a1a3ed5..0733c48d 100644 --- a/packages/@pollyjs/persister/src/index.js +++ b/packages/@pollyjs/persister/src/index.js @@ -1,5 +1,5 @@ import stringify from 'fast-json-stable-stringify'; -import { ACTIONS, assert } from '@pollyjs/utils'; +import { ACTIONS, assert, getFactoryId } from '@pollyjs/utils'; import HAR from './har'; import Entry from './har/entry'; @@ -27,11 +27,11 @@ export default class Persister { } get options() { - const { id } = this.constructor; - return { ...(this.defaultOptions || {}), - ...((this.polly.config.persisterOptions || {})[id] || {}) + ...((this.polly.config.persisterOptions || {})[ + getFactoryId(this.constructor) + ] || {}) }; } @@ -53,7 +53,7 @@ export default class Persister { const creator = { name: CREATOR_NAME, version: this.polly.constructor.VERSION, - comment: `${this.constructor.type}:${this.constructor.id}` + comment: `${this.constructor.type}:${getFactoryId(this.constructor)}` }; for (const [recordingId, { name, requests }] of this.pending) { @@ -177,7 +177,7 @@ export default class Persister { assert(message, ...args) { assert( - `[${this.constructor.type}:${this.constructor.id}] ${message}`, + `[${this.constructor.type}:${getFactoryId(this.constructor)}] ${message}`, ...args ); } diff --git a/packages/@pollyjs/utils/src/index.js b/packages/@pollyjs/utils/src/index.js index 0fd535f8..1062bd93 100644 --- a/packages/@pollyjs/utils/src/index.js +++ b/packages/@pollyjs/utils/src/index.js @@ -13,3 +13,5 @@ export { default as PollyError } from './utils/polly-error'; export { default as Serializers } from './utils/serializers'; export { default as URL } from './utils/url'; + +export { default as getFactoryId } from './utils/get-factory-id'; diff --git a/packages/@pollyjs/utils/src/utils/get-factory-id.js b/packages/@pollyjs/utils/src/utils/get-factory-id.js new file mode 100644 index 00000000..a5068fbd --- /dev/null +++ b/packages/@pollyjs/utils/src/utils/get-factory-id.js @@ -0,0 +1,11 @@ +export default function getFactoryId(Factory) { + if (Factory.hasOwnProperty('id')) { + return Factory.id; + } + + console.warn( + `[Polly] ${Factory.name} "name" is deprecated and has been renamed to "id".` + ); + + return Factory.name; +}