Skip to content

Commit

Permalink
Change scope.remove to also remove interceptor from scope.interceptors
Browse files Browse the repository at this point in the history
Note: for consistency between scope.interceptors and scope.keyedInterceptors,
interceptors are now only pushed to scope.interceptors when they are added,
not when they are created. That guarantees both collections are consistent.
  • Loading branch information
mbargiel committed May 21, 2022
1 parent ad84a6d commit d4ac1cf
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/scope.js
Expand Up @@ -68,6 +68,7 @@ class Scope extends EventEmitter {
this.keyedInterceptors[key] = []
}
this.keyedInterceptors[key].push(interceptor)
this.interceptors.push(interceptor)
addInterceptor(
this.basePath,
interceptor,
Expand All @@ -84,6 +85,7 @@ class Scope extends EventEmitter {
if (arr.length === 0) {
delete this.keyedInterceptors[key]
}
this.interceptors.splice(this.interceptors.indexOf(interceptor), 1)
}
}

Expand All @@ -96,7 +98,6 @@ class Scope extends EventEmitter {
interceptorOptions
)

this.interceptors.push(ic)
return ic
}

Expand Down
28 changes: 28 additions & 0 deletions tests/test_remove_interceptor.js
Expand Up @@ -55,6 +55,34 @@ describe('`removeInterceptor()`', () => {
expect(scope.pendingMocks()).to.deep.equal([])
})

it('reflects the removal in `interceptors` of its scope', () => {
const givenInterceptor = nock('http://example.test').get('/somepath')
const scope = givenInterceptor.reply(200, 'hey')

expect(scope.interceptors).to.deep.equal([
givenInterceptor,
])

expect(nock.removeInterceptor(givenInterceptor)).to.be.true()

expect(scope.interceptors).to.deep.equal([])
})

it('reflects the removal in `keyedInterceptors` of its scope', () => {
const givenInterceptor = nock('http://example.test').get('/somepath')
const scope = givenInterceptor.reply(200, 'hey')

expect(scope.keyedInterceptors).to.deep.equal({
'GET http://example.test:80/somepath': [
givenInterceptor,
]
})

expect(nock.removeInterceptor(givenInterceptor)).to.be.true()

expect(scope.keyedInterceptors).to.deep.equal({})
})

it('removes given interceptor for https', async () => {
const givenInterceptor = nock('https://example.test').get('/somepath')
const scope = givenInterceptor.reply(200, 'hey')
Expand Down
23 changes: 22 additions & 1 deletion tests/test_scope.js
Expand Up @@ -65,18 +65,39 @@ describe('`Scope#remove()`', () => {
expect(scope.activeMocks()).to.deep.equal([])
})

it('removes the interceptor from its collections', () => {
const scope = nock('http://example.test').get('/').reply(200)
const key = 'GET http://example.test:80/'
const interceptor = scope.interceptors[0]

// Confidence check.
expect(scope.activeMocks()).to.deep.equal([key])
expect(scope.keyedInterceptors).to.deep.equal({ [key]: [interceptor] })

// Act.
scope.remove(key, scope.interceptors[0])

// Assert.
expect(scope.interceptors).to.deep.equal([])
expect(scope.keyedInterceptors).to.deep.equal({})
})

it('when the key is nonexistent, does nothing', () => {
const scope = nock('http://example.test').get('/').reply(200)
const key = 'GET http://example.test:80/'
const interceptor = scope.interceptors[0]

// Confidence check.
expect(scope.activeMocks()).to.deep.equal([key])
expect(scope.keyedInterceptors).to.deep.equal({ [key]: [interceptor] })

// Act.
scope.remove('GET http://bogus.test:80/', scope.interceptors[0])
scope.remove('GET http://bogus.test:80/', interceptor)

// Assert.
expect(scope.activeMocks()).to.deep.equal([key])
expect(scope.interceptors).to.deep.equal([interceptor])
expect(scope.keyedInterceptors).to.deep.equal({ [key]: [interceptor] })
})
})

Expand Down

0 comments on commit d4ac1cf

Please sign in to comment.