简体   繁体   中英

Enzyme restore getEelemenById before each test

I stub getElementById in beforeEach , and want to restore it before another test and stub again with anothter returns value. Because now I recieve error TypeError: Attempted to wrap getElementById which is already wrapped

 let loginUrl = 'loginUrl'
    const url = '/app/auth'
    const textContent = `{"${loginUrl}":"${url}"}`
    let htmlDecode

    describe('identityServer', () => {

        beforeEach(() => {
            htmlDecode = sinon.stub().returns(textContent)
            sinon.stub(document, 'getElementById').returns({textContent})
            sinon.stub(htmlEncoder, 'Encoder').returns({htmlDecode: () => htmlDecode})

            identityServerModel()
        })

        it('should return correct model for IdentityServer', () => {
            window.identityServer.getModel().should.deep.equal({[loginUrl]: url})
        })
    })

    describe('identityServer', () => {

        beforeEach(() => {
            htmlDecode = sinon.stub().returns(textContent)
            sinon.stub(document, 'getElementById').returns({innerHTML: textContent})
            sinon.stub(htmlEncoder, 'Encoder').returns({htmlDecode: () => htmlDecode})

            identityServerModel()
        })
        it('should return correct model using serialization HTML from innerHTML property when textContent is undefined', () => {
            window.identityServer.getModel().should.deep.equal({[loginUrl]: url})
        })
    })

Try add:

afterEach(() => {
    document.getElementById.restore();
})

into every describe(...) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM