如何戏弄测试DPI

huangapple go评论73阅读模式
英文:

How in jest test DPI

问题

I have a function that changes the size of an image depending on window.devicePixelRatio, and now I want to properly test it.

const wrapper = mount(myComponent, {
    propsData: {
      size: {
        height: 500,
        width: 500,
      },
      format: 'jpeg',
      filename: 'path/to/image.jpg',
      dpi: 1,
    },
  })

it('check if higher dpi generates double-sized images', () => {
 wrapper.setProps({ size: { height: 600, width: 400 }, format: 'jpeg', filename: 'www.path/to/image.jpg', quality: 80, dpi: 2 })
  expect(imgWrapper.attributes('height')).toBe('600')
  expect(imgWrapper.attributes('width')).toBe('400')
  expect(imgWrapper.attributes('src')).toBe('www.yoursUrl.com/w=800,h=1200,f=jpeg,q=80/www.path/to/image.jpg')
})

And this is what the test shows:

Expected: "www.yoursUrl.com/w=800,h=1200,f=jpeg,q=80/www.path/to/image.jpg"
Received: "www.yoursUrl.com/w=400,h=600,f=jpeg,q=80/www.path/to/image.jpg"

Thanks for any ideas.

Code in component:

Props:

props: {
 dpi: {
      type: Number,
      default: 1,
 },
}

Methods:

isHigherDPI() {
  return window.devicePixelRatio > this.dpi
}

imageRouteFinalImage() {
      if (this.isHigherDPI()) {
        width = this.size.width * 2
        height = this.size.height * 2
      }

      return `www.yoursUrl.com/w=${width},h=${height},/www.path/to/image.jpg`
}
英文:

I have function that change size of image depending on window.devicePixelRatio

and now i want to proper test it.

const wrapper = mount(myComponent, {
    propsData: {
      size: {
        height: 500,
        width: 500,
      },
      format: 'jpeg',
      filename: 'path/to/image.jpg',
      dpi: 1,
    },
  })

it('check if higer dpi generate double sized images', () => {
 wrapper.setProps({ size: { height: 600, width: 400 }, format: 'jpeg', filename: 'www.path/to/image.jpg', quality: 80, dpi: 2 })
  expect(imgWrapper.attributes('height')).toBe('600')
  expect(imgWrapper.attributes('width')).toBe('400')
  expect(imgWrapper.attributes('src')).toBe('www.yoursUrl.com/w=800,h=1200,f=jpeg,q=80/www.path/to/image.jpg')
})

and that what test show

Expected: "www.yoursUrl.com/w=800,h=1200,f=jpeg,q=80/www.path/to/image.jpg"
Received: "www.yoursUrl.com/w=400,h=600,f=jpeg,q=80/www.path/to/image.jpg"

thx for ideas

Code in compoonent

Props

props: {
 dpi: {
      type: Number,
      default: 1,
 },
}

Methods

isHigherDPI() {
  return window.devicePixelRatio > this.dpi
}

imageRouteFinalImage() {
      if (this.isHigherDPI()) {
        width = this.size.width * 2
        height = this.size.height * 2
      }

      return `${www.yoursUrl.com/w=${width},h=${height},/www.path/to/image.jpg`
},

</details>


# 答案1
**得分**: 3

`window.devicePixelRatio`已经可写,所以你可以在运行测试之前简单地设置它:

```js
describe('MyComponent.vue', () => {
  it('检查是否为高dpi...', () => {
    window.devicePixelRatio = 3
    const wrapper = shallowMount(MyComponent, { propsData: {/*...*/} })
    expect(wrapper.vm.imgSrc).toEqual('foo.jpg?dpi=high')
  })
})

另一方面,如果你需要验证已读取window.devicePixelRatio,你可以监听window对象,并模拟目标属性:

describe('MyComponent.vue', () => {
  it('检查是否为高dpi...', () => {
    const devicePixelRatioGetter = jest.fn().mockReturnValue(3)

    jest.spyOn(global, 'window', 'get').mockImplementation(() => Object.defineProperty({}, 'devicePixelRatio', {
      get: devicePixelRatioGetter
    }))

    const wrapper = shallowMount(MyComponent, { propsData: {/*...*/} })
    expect(wrapper.vm.imgSrc).toEqual('foo.jpg?dpi=high')
    expect(devicePixelRatioGetter).toHaveBeenCalled()
  })
})
英文:

window.devicePixelRatio is already writable, so you could simply set it before you run your test:

describe(&#39;MyComponent.vue&#39;, () =&gt; {
  it(&#39;check if higher dpi...&#39;, () =&gt; {
    window.devicePixelRatio = 3
    const wrapper = shallowMount(MyComponent, { propsData: {/*...*/} })
    expect(wrapper.vm.imgSrc).toEqual(&#39;foo.jpg?dpi=high&#39;)
  })
})

On the other hand, if you need to verify window.devicePixelRatio was read, you could spy on the window object, and mock the target property:

describe(&#39;MyComponent.vue&#39;, () =&gt; {
  it(&#39;check if higher dpi...&#39;, () =&gt; {
    const devicePixelRatioGetter = jest.fn().mockReturnValue(3)

    jest.spyOn(global, &#39;window&#39;, &#39;get&#39;).mockImplementation(() =&gt; Object.defineProperty({}, &#39;devicePixelRatio&#39;, {
      get: devicePixelRatioGetter
    }))

    const wrapper = shallowMount(MyComponent, { propsData: {/*...*/} })
    expect(wrapper.vm.imgSrc).toEqual(&#39;foo.jpg?dpi=high&#39;)
    expect(devicePixelRatioGetter).toHaveBeenCalled()
  })
})

huangapple
  • 本文由 发表于 2020年1月3日 23:09:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580920.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定