{"name":"jest-canvas-mock","version":"2.0.0-alpha.0","description":"A module to mock canvas in Jest.","main":"lib/index.js","scripts":{"test":"jest --no-cache","build":"babel src --out-dir lib","coveralls":"cat ./coverage/lcov.info | coveralls"},"repository":{"type":"git","url":"git+https://github.com/hustcc/jest-canvas-mock.git"},"keywords":["mock","jest","jest-mock","echarts","canvas","test","unit"],"author":{"name":"hustcc"},"license":"ISC","bugs":{"url":"https://github.com/hustcc/jest-canvas-mock/issues"},"homepage":"https://github.com/hustcc/jest-canvas-mock#readme","devDependencies":{"@babel/cli":"^7.2.3","@babel/plugin-proposal-class-properties":"^7.3.0","@babel/preset-env":"^7.3.1","babel-jest":"^24.0.0","babel-plugin-version":"^0.2.3","coveralls":"^3.0.2","jest":"^24.0.0"},"jest":{"collectCoverage":true,"collectCoverageFrom":["src/classes/**/*.js"],"setupFiles":["./src/index.js"]},"dependencies":{"cssfontparser":"^1.2.1","parse-color":"^1.0.0"},"gitHead":"045eb6a93fa3fc26b8a0e052cfeda856743fc1c0","_id":"jest-canvas-mock@2.0.0-alpha.0","_npmVersion":"6.4.1","_nodeVersion":"10.10.0","_npmUser":{"name":"atool","email":"i@hust.cc"},"dist":{"integrity":"sha512-V/IalsZgYGATwzJtUG3WMkjoE0OH0sqv9K/+CguTKdiqnrKwpIXpqDsASobWRGXkuQie2rmIntAQ0PRSmLVSzw==","shasum":"18ab0ede01bf16244143e04de5eac43871f7176b","tarball":"http://registry-npm.ywwl.com/jest-canvas-mock/download/jest-canvas-mock-2.0.0-alpha.0.tgz","fileCount":13,"unpackedSize":61334,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJcUTPwCRA9TVsSAnZWagAAPkoQAKIMoV2lIi/8JI51rLRH\nqx+GX5Z2WiyaZNW25Bd8ocDVzm13/hcGPPyn2/Ux4fas4rnXNXpxfAasIfSy\n5UA9VsjL5QofsFkA1MgMDXjNqp/PC9E51dgVnv+QvE+PuJvOPEpM4sWg/iaa\ninqiB64JwMaWurcl7NaBOYXFOkO24xduVDyOoA9ctwy7XxF4pPgWbrjBBzEZ\n8xncLWfZMam3JMVDjvLD1zj9nf38ZVg6cwUMb0JSFXGRu+G7rCOInWTRjmJz\nNkVnxtdGuFB57ELKjjk0gTMMGWiBwmVNghIOtjNCDD5HEXzIZ7CPCmS+4mmZ\nElnI0tPU7RVEFewulrxP+3b9m2fN7WJLQMWGUab0Ekth4TNGJHU1uOQ94WaQ\nYg0lkqLr6UxaVoqMnyieF+kCdSf/Je4O+bavAgml//J20tVaIgHI84Q5EdoU\ndplxESPAgN7miCd2UXsF0NwI7WB07FNk6oLnexg/0gIpjQOhxPHeHogysn8/\nkO6F/MzLzrKiI6XdemvpTYnVWmezd1BsvhdKLMO4IOSkR1u90GTb5c1IePvf\ntvLYTugvvw8juYbOMrgD+wmT4/qsUMeMzt6YIFVRujZRvI5AC0SKokCHLrRn\n1qJKMWVo1Gw1v2wCKZXPHowxkLgkmK7QfI1MKJ2Q3YTyVXqYzkN9X1aKff1F\nCcG8\r\n=saoN\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQC1DelKlfso/lVdWbcuwfkAugYNwKni2Md996bDMEcqHgIgMtkdt5zyXd6xZKYYWurek5EJlqK4FygocHr99Z/pDFg="}],"size":11141,"noattachment":false,"key":"/jest-canvas-mock/-/jest-canvas-mock-2.0.0-alpha.0.tgz"},"maintainers":[{"name":"atool","email":"i@atool.org"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/jest-canvas-mock_2.0.0-alpha.0_1548825583423_0.7556433828568978"},"_hasShrinkwrap":false,"readme":"# jest-canvas-mock\n\n> Mock `canvas` when run unit test cases with jest. For more browser environment, you can use [jest-electron](https://github.com/hustcc/jest-electron) for real browser runtime.\n\n[![Build Status](https://github.com/hustcc/jest-canvas-mock/workflows/build/badge.svg)](https://github.com/hustcc/jest-canvas-mock/actions/workflows/build.yml)\n[![Coverage Status](https://coveralls.io/repos/github/hustcc/jest-canvas-mock/badge.svg?branch=master)](https://coveralls.io/github/hustcc/jest-canvas-mock)\n[![npm](https://img.shields.io/npm/v/jest-canvas-mock.svg)](https://www.npmjs.com/package/jest-canvas-mock)\n[![npm](https://img.shields.io/npm/dm/jest-canvas-mock.svg)](https://www.npmjs.com/package/jest-canvas-mock)\n[![Mentioned in Awesome Jest](https://awesome.re/mentioned-badge.svg)](https://github.com/jest-community/awesome-jest)\n\n## Install\n\nThis should only be installed as a development dependency (`devDependencies`) as it is only designed for testing.\n\n```bash\nnpm i --save-dev jest-canvas-mock\n```\n\n## Setup\n\nIn your `package.json` under the `jest`, create a `setupFiles` array and add `jest-canvas-mock` to the array.\n\n```json\n{\n  \"jest\": {\n    \"setupFiles\": [\"jest-canvas-mock\"]\n  }\n}\n```\n\nIf you already have a `setupFiles` attribute you can also append `jest-canvas-mock` to the array.\n\n```json\n{\n  \"jest\": {\n    \"setupFiles\": [\"./__setups__/other.js\", \"jest-canvas-mock\"]\n  }\n}\n```\n\nMore about in [configuration section](https://facebook.github.io/jest/docs/en/configuration.html#content).\n\n## Setup file\n\nAlternatively you can create a new setup file which then requires this module or\nadd the `require` statement to an existing setup file.\n\n`__setups__/canvas.js`\n\n```js\nimport 'jest-canvas-mock';\n// or\nrequire('jest-canvas-mock');\n```\n\nAdd that file to your `setupFiles` array:\n\n```json\n\"jest\": {\n  \"setupFiles\": [\n    \"./__setups__/canvas.js\"\n  ]\n}\n```\n\n## Reset\n\nIf you reset the jest mocks (for example, with `jest.resetAllMocks()`), you can\ncall `setupJestCanvasMock()` to re-create it.\n\n```\nimport { setupJestCanvasMock } from 'jest-canvas-mock';\n\nbeforeEach(() => {\n  jest.resetAllMocks();\n  setupJestCanvasMock();\n});\n```\n\n## Mock Strategy\n\nThis mock strategy implements all the canvas functions and actually verifies the parameters. If a\nknown condition would cause the browser to throw a `TypeError` or a `DOMException`, it emulates the\nerror. For instance, the `CanvasRenderingContext2D#arc` function will throw a `TypeError` if the\nradius is negative, or if it was not provided with enough parameters.\n\n```ts\n// arc throws a TypeError when the argument length is less than 5\nexpect(() => ctx.arc(1, 2, 3, 4)).toThrow(TypeError);\n\n// when radius is negative, arc throws a dom exception when all parameters are finite\nexpect(() => ctx.arc(0, 0, -10, 0, Math.PI * 2)).toThrow(DOMException);\n```\n\nThe function will do `Number` type coercion and verify the inputs exactly like the browser does. So\nthis is valid input.\n\n```ts\nexpect(() => ctx.arc('10', '10', '20', '0', '6.14')).not.toThrow();\n```\n\nAnother part of the strategy is to validate input types. When using the\n`CanvasRenderingContext2D#fill` function, if you pass it an invalid `fillRule` it will throw a\n`TypeError` just like the browser does.\n\n```ts\nexpect(() => ctx.fill('invalid!')).toThrow(TypeError);\nexpect(() => ctx.fill(new Path2D(), 'invalid!')).toThrow(TypeError);\n```\n\nWe try to follow the ECMAScript specification as closely as possible.\n\n## Snapshots\n\nThere are multiple ways to validate canvas state using snapshots. There are currently three methods\nattached to the `CanvasRenderingContext2D` class. The first way to use this feature is by using the\n`__getEvents` method.\n\n```ts\n/**\n * In order to see which functions and properties were used for the test, you can use `__getEvents`\n * to gather this information.\n */\nconst events = ctx.__getEvents();\n\nexpect(events).toMatchSnapshot(); // jest will assert the events match the snapshot\n```\n\nThe second way is to inspect the current path associated with the context.\n\n```ts\nctx.beginPath();\nctx.arc(1, 2, 3, 4, 5);\nctx.moveTo(6, 7);\nctx.rect(6, 7, 8, 9);\nctx.closePath();\n\n/**\n * Any method that modifies the current path (and subpath) will be pushed to an event array. When\n * using the `__getPath` method, that array will sliced and usable for snapshots.\n */\nconst path = ctx.__getPath();\nexpect(path).toMatchSnapshot();\n```\n\nThe third way is to inspect all of the successful draw calls submitted to the context.\n\n```ts\nctx.drawImage(img, 0, 0);\n\n/**\n * Every drawImage, fill, stroke, fillText, or strokeText function call will be logged in an event\n * array. This method will return those events here for inspection.\n */\nconst calls = ctx.__getDrawCalls();\nexpect(calls).toMatchSnapshot();\n```\n\nIn some cases it may be useful to clear the events or draw calls that have already been logged.\n\n```ts\n// Clear events\nctx.__clearEvents();\n\n// Clear draw calls\nctx.__clearDrawCalls();\n```\n\nFinally, it's possible to inspect the clipping region calls by using the `__getClippingRegion`\nfunction.\n\n```ts\nconst clippingRegion = ctx.__getClippingRegion();\nexpect(clippingRegion).toMatchSnapshot();\n```\n\nThe clipping region cannot be cleared because it's based on the stack values and when the `.clip()`\nfunction is called.\n\n## Override default mock return value\n\nYou can override the default mock return value in your test to suit your need. For example, to override return value of `toDataURL`:\n\n```ts\ncanvas.toDataURL.mockReturnValueOnce(\n  'data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='\n);\n```\n\n## Contributors\n\n- [@hustcc](https://github.com/hustcc)\n- [@jtenner](https://github.com/jtenner)\n- [@evanoc0](https://github.com/evanoc0)\n- [@lekha](https://github.com/lekha)\n- [@yonatankra](https://github.com/yonatankra)\n- [@LitoMore](https://github.com/LitoMore)\n- [@hrd543](https://github.com/hrd543)\n- [@danielrentz](https://github.com/danielrentz)\n\n## License\n\nMIT@[hustcc](https://github.com/hustcc).\n","publish_time":1548825583684,"_cnpm_publish_time":1548825583684,"dist-tags":{"latest":"2.5.8"}}