How to use expectedContext method in fast-check-monorepo

Best JavaScript code snippet using fast-check-monorepo

vlc-player.test.ts

Source:vlc-player.test.ts Github

copy

Full Screen

1import 'mocha'2import { expect } from 'chai'3import * as sinon from 'sinon'4import { Duplex } from 'stream'5import { Context, Player, VLCPlayer } from '../../player'6import { Track } from '../../../shared'7import { delay } from '../utils'8const Util = require('util')9describe('VLC player tests', () => {10 let vlcPlayer: Player11 let fakeProcess12 let expectedContext: Context13 before(() => {14 fakeProcess = {15 stdin: new Duplex({16 read(size?) {},17 write(chunk, encoding?, callback?) { callback() }18 }),19 stdout: new Duplex({20 read(size?) {},21 write(chunk, encoding?, callback?) {}22 })23 }24 })25 afterEach(() => {26 sinon.restore()27 })28 describe('Player tests', () => {29 it('Should start the VLC player', async function () {30 this.timeout(5000)31 let fake = sinon.fake.returns(fakeProcess)32 expectedContext = new Context()33 vlcPlayer = new VLCPlayer('test', fake, new Context(), [], '')34 vlcPlayer.start()35 /* Init */36 fakeProcess.stdout.push("VLC media player")37 fakeProcess.stdout.push(" 3.0.4 ")38 fakeProcess.stdout.push("FAKE VLC\r\n")39 fakeProcess.stdout.push("Command Line Interface initialized. Type `help' for help.\r\n")40 await delay(1000)41 /* VLC initialization */42 expectedContext.setTitle('My awesome movie')43 fakeProcess.stdout.push(`${expectedContext.getTitle()}\r\n`)44 /* Pause mode */45 expectedContext.setPlaying(true)46 expectedContext.stopPlaying()47 fakeProcess.stdout.push("> ")48 await delay(1000)49 /* Title */50 fakeProcess.stdout.push(`${expectedContext.getTitle()}\r\n`)51 /* Length */52 expectedContext.setLength(42)53 fakeProcess.stdout.push(`${expectedContext.getLength()}\r\n`)54 /* Time */55 expectedContext.setTime(0)56 fakeProcess.stdout.push(`${expectedContext.getTime()}\r\n`)57 /* Volume */58 expectedContext.setVolume(100)59 fakeProcess.stdout.push(`${expectedContext.getVolume()}\r\n`)60 /* Video tracks */61 expectedContext.setVideoTracks([])62 fakeProcess.stdout.push("+----[ video-es ]\r\n")63 fakeProcess.stdout.push("+----[ end of video-es ]\r\n")64 /* Audio tracks */65 expectedContext.setAudioTracks([])66 fakeProcess.stdout.push("+----[ audio-es ]\r\n")67 fakeProcess.stdout.push("+----[ end of audio-es ]\r\n")68 /* Subtitle tracks */69 expectedContext.setSubtitleTracks([])70 fakeProcess.stdout.push("+----[ spu-es ]\r\n")71 fakeProcess.stdout.push("+----[ end of spu-es ]\r\n")72 await delay(1000)73 })74 it ('Should set the player in play mode', async function () {75 let promise = vlcPlayer.play()76 fakeProcess.stdout.push("> ")77 let result = await promise78 expectedContext.startPlaying()79 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())80 })81 it ('Should set the player in pause mode', async function () {82 let promise = vlcPlayer.pause()83 fakeProcess.stdout.push("> ")84 let result = await promise85 expectedContext.stopPlaying()86 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())87 })88 it ("Should get the media's title", async function () {89 let promise = vlcPlayer.getTitle()90 fakeProcess.stdout.push("Testing VLC player ...\r\n> ")91 let result = await promise92 expectedContext.setTitle('Testing VLC player ...')93 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())94 })95 it ("Should get the media's length", async function () {96 let promise = vlcPlayer.getLength()97 fakeProcess.stdout.push("3293\r\n")98 let result = await promise99 expectedContext.setLength(3293)100 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())101 })102 it ('Should update the volume', async function () {103 let promise = vlcPlayer.getVolume()104 fakeProcess.stdout.push("130\r\n")105 let result = await promise106 expectedContext.setVolume(130)107 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())108 })109 it ('Should update the volume, with another syntax', async function () {110 let promise = vlcPlayer.getVolume()111 fakeProcess.stdout.push("272.0\r\n")112 let result = await promise113 expectedContext.setVolume(272)114 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())115 })116 it ('Should update the volume, with ArchLinux syntax', async function () {117 let promise = vlcPlayer.getVolume()118 fakeProcess.stdout.push("320,0\r\n")119 let result = await promise120 expectedContext.setVolume(320)121 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())122 })123 it ('Should set the volume', async function () {124 let promise = vlcPlayer.setVolume(80)125 fakeProcess.stdout.push("> ")126 let result = await promise127 expectedContext.setVolume(80)128 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())129 })130 it ('Should increase the volume', async function () {131 let promise = vlcPlayer.volumeUp()132 fakeProcess.stdout.push("( audio volume: 90 )\r\n")133 let result = await promise134 expectedContext.setVolume(90)135 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())136 })137 it ('Should increase the volume, with another syntax', async function () {138 let promise = vlcPlayer.volumeUp()139 fakeProcess.stdout.push("( audio volume: 102.0 )\r\n")140 let result = await promise141 expectedContext.setVolume(102)142 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())143 })144 it ('Should increase the volume, with ArchLinux syntax', async function () {145 let promise = vlcPlayer.volumeUp()146 fakeProcess.stdout.push("( audio volume: 115,0 )\r\n")147 let result = await promise148 expectedContext.setVolume(115)149 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())150 })151 it ('Should decrease the volume', async function () {152 let promise = vlcPlayer.volumeDown()153 fakeProcess.stdout.push("( audio volume: 70 )\r\n")154 let result = await promise155 expectedContext.setVolume(70)156 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())157 })158 it ('Should decrease the volume, with another syntax', async function () {159 let promise = vlcPlayer.volumeDown()160 fakeProcess.stdout.push("( audio volume: 51.0 )\r\n")161 let result = await promise162 expectedContext.setVolume(51)163 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())164 })165 it ('Should decrease the volume, with ArchLinux syntax', async function () {166 let promise = vlcPlayer.volumeDown()167 fakeProcess.stdout.push("( audio volume: 38,0 )\r\n")168 let result = await promise169 expectedContext.setVolume(38)170 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())171 })172 it ('Should mute the media', async function () {173 let promise = vlcPlayer.mute()174 fakeProcess.stdout.push("> ")175 let result = await promise176 expectedContext.setVolume(0)177 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())178 })179 it ('Should get the current time', async function () {180 let promise = vlcPlayer.getTime()181 fakeProcess.stdout.push("10\r\n> ")182 let result = await promise183 expectedContext.setTime(10)184 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())185 })186 it ('Should set the current time', async function () {187 let promise = vlcPlayer.setTime(45)188 fakeProcess.stdout.push("> ")189 let result = await promise190 expectedContext.setTime(45)191 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())192 })193 it ('Should go forward', async function () {194 let promise = vlcPlayer.addTime(10)195 fakeProcess.stdout.push("> ")196 let result = await promise197 expectedContext.setTime(55)198 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())199 })200 it ('Should go backward', async function () {201 let promise = vlcPlayer.addTime(-15)202 fakeProcess.stdout.push("> ")203 let result = await promise204 expectedContext.setTime(40)205 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())206 })207 it ("Should not set the time before the media's start", async function () {208 let promise = vlcPlayer.setTime(-5)209 fakeProcess.stdout.push("> ")210 let result = await promise211 expectedContext.setTime(0)212 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())213 })214 it ("Should not set the time beyond the media's length", async function () {215 let promise = vlcPlayer.setTime(expectedContext.getLength() + 6)216 fakeProcess.stdout.push("> ")217 let result = await promise218 expectedContext.setTime(expectedContext.getLength())219 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())220 })221 it ("Should not go before the media's start", async function () {222 let timePromise = vlcPlayer.setTime(3)223 fakeProcess.stdout.push("> ")224 await timePromise225 expectedContext.setTime(3)226 let testPromise = vlcPlayer.addTime(-10)227 fakeProcess.stdout.push("> ")228 let result = await testPromise229 /* The time should be set at 0 since it can't go in negative */230 expectedContext.setTime(0)231 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())232 })233 it ("Should not go beyond the media's length", async function () {234 let timePromise = vlcPlayer.setTime(expectedContext.getLength() - 6)235 fakeProcess.stdout.push("> ")236 await timePromise237 expectedContext.setTime(expectedContext.getLength() - 6)238 let testPromise = vlcPlayer.addTime(10)239 fakeProcess.stdout.push("> ")240 let result = await testPromise241 /* The time should be limited to the media's length */242 expectedContext.setTime(expectedContext.getLength())243 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())244 })245 it ('Should do nothing when the user tries to set the time to the current time', async function () {246 /* Set the media time (and ignore the result since we don't need it) */247 let begin = vlcPlayer.setTime(0)248 fakeProcess.stdout.push("> ")249 await begin250 expectedContext.setTime(0)251 /* This promise should be fulfilled immediately */252 let result = await vlcPlayer.addTime(-10)253 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())254 })255 it ('Should handle partials reads', async function () {256 this.timeout(5000)257 let videoTrack = [258 { id: -1, title: undefined, language: 'Disable', selected: false },259 { id: 0, title: undefined, language: 'Track 1', selected: true }260 ]261 let audioTrack = [262 { id: -1, title: undefined, language: 'Désactiver', selected: false },263 { id: 1, title: 'E-AC3 5.1 @ 640 Kbps', language: 'Anglais', selected: true },264 { id: 2, title: 'E-AC3 5.1 @ 640 Kbps', language: 'Français', selected: false }265 ]266 let subtitleTrack = [267 { id: -1, title: undefined, language: 'Désactiver', selected: false },268 { id: 3, title: 'Z1 (srt)', language: 'Français', selected: true },269 { id: 4, title: undefined, language: 'Sous-titrage malentendants 1', selected: false },270 { id: 5, title: undefined, language: 'Sous-titrage malentendants 2', selected: false },271 { id: 6, title: undefined, language: 'Sous-titrage malentendants 3', selected: false },272 { id: 7, title: undefined, language: 'Sous-titrage malentendants 4', selected: false }273 ]274 let promiseAll = vlcPlayer.getMediaInformations()275 /* Title */276 fakeProcess.stdout.push(`${expectedContext.getTitle()}\r\n`)277 await delay(300)278 /* Length */279 fakeProcess.stdout.push(`${expectedContext.getLength()}\r\n`)280 await delay(300)281 /* Time */282 fakeProcess.stdout.push(`${expectedContext.getTime()}\r\n`)283 await delay(300)284 /* Volume */285 fakeProcess.stdout.push(`${expectedContext.getVolume()}\r\n`)286 await delay(100)287 /* Video tracks */288 fakeProcess.stdout.push("+----[ video-es ]\r\n")289 await delay(200)290 fakeProcess.stdout.push("| -1 - Disable\r\n")291 await delay(200)292 fakeProcess.stdout.push("| 0 - Track 1 *\r\n")293 await delay(200)294 fakeProcess.stdout.push("+----[ end of video-es ]\r\n")295 await delay(200)296 /* Audio tracks */297 fakeProcess.stdout.push("+----[ audio-es ]\r\n")298 await delay(200)299 fakeProcess.stdout.push("| -1 - Désactiver\r\n")300 await delay(200)301 fakeProcess.stdout.push("| 1 - E-AC3 5.1 @ 640 Kbps - [Anglais] *\r\n")302 await delay(200)303 fakeProcess.stdout.push("| 2 - E-AC3 5.1 @ 640 Kbps - [Français]\r\n")304 await delay(200)305 fakeProcess.stdout.push("+----[ end of audio-es ]\r\n")306 await delay(200)307 /* Subtitle tracks */308 fakeProcess.stdout.push("+----[ spu-es ]\r\n")309 await delay(200)310 fakeProcess.stdout.push("| -1 - Désactiver\r\n")311 await delay(200)312 fakeProcess.stdout.push("| 3 - Z1 (srt) - [Français] *\r\n")313 await delay(200)314 fakeProcess.stdout.push("| 4 - Sous-titrage malentendants 1\r\n")315 await delay(200)316 fakeProcess.stdout.push("| 5 - Sous-titrage malentendants 2\r\n")317 await delay(200)318 fakeProcess.stdout.push("| 6 - Sous-titrage malentendants 3\r\n")319 await delay(200)320 fakeProcess.stdout.push("| 7 - Sous-titrage malentendants 4\r\n")321 await delay(200)322 fakeProcess.stdout.push("+----[ end of spu-es ]\r\n")323 await delay(300)324 fakeProcess.stdout.push("> ")325 let resultPromiseAll = await promiseAll326 let result = resultPromiseAll[6]327 expectedContext.setVideoTracks(videoTrack)328 expectedContext.setAudioTracks(audioTrack)329 expectedContext.setSubtitleTracks(subtitleTrack)330 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())331 })332 it ('Should get the title with partial reads', async function () {333 let promise = vlcPlayer.getTitle()334 fakeProcess.stdout.push("My awesome ")335 await delay(500)336 fakeProcess.stdout.push("movie")337 await delay(500)338 fakeProcess.stdout.push("\r\n> ")339 let result = await promise340 expectedContext.setTitle("My awesome movie")341 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())342 })343 it ('Should decrease the volume, with partial reads', async function () {344 let promise = vlcPlayer.volumeDown()345 fakeProcess.stdout.push("( audio volume: ")346 await delay(500)347 fakeProcess.stdout.push("20 )\r\n> ")348 let result = await promise349 expectedContext.setVolume(20)350 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())351 })352 })353 describe('Track tests', () => {354 let emptyTracks: Track[]355 let defaultTracks: Track[]356 let testTracks1: Track[]357 let testTracks2: Track[]358 let testTracks3: Track[]359 let testTracks4: Track[]360 let testTracks5: Track[]361 let testTracks6: Track[]362 let testTracks7: Track[]363 let testTracks8: Track[]364 beforeEach (() => {365 emptyTracks = []366 defaultTracks = [367 { id: -1, title: undefined, language: 'Disable', selected: false },368 { id: 0, title: undefined, language: 'Track 1', selected: true }369 ]370 testTracks1 = [371 { id: -1, title: undefined, language: 'Désactiver', selected: false },372 { id: 1, title: 'E-AC3 5.1 @ 640 Kbps', language: 'Anglais', selected: true },373 { id: 2, title: 'E-AC3 5.1 @ 640 Kbps', language: 'Français', selected: false }374 ]375 testTracks2 = [376 { id: -1, title: undefined, language: 'Désactiver', selected: false },377 { id: 3, title: 'Z1 (srt)', language: 'Français', selected: true },378 { id: 4, title: undefined, language: 'Sous-titrage malentendants 1', selected: false },379 { id: 5, title: undefined, language: 'Sous-titrage malentendants 2', selected: false },380 { id: 6, title: undefined, language: 'Sous-titrage malentendants 3', selected: false },381 { id: 7, title: undefined, language: 'Sous-titrage malentendants 4', selected: false }382 ]383 testTracks3 = [384 { id: -1, title: undefined, language: 'Désactiver', selected: false },385 { id: 0, title: 'Piste 1', language: 'Anglais', selected: true }386 ]387 testTracks4 = [388 { id: -1, title: undefined, language: 'Désactiver', selected: false },389 { id: 1, title: 'FR', language: 'Français', selected: true },390 { id: 2, title: 'EN', language: 'Anglais', selected: false }391 ]392 testTracks5 = [393 { id: -1, title: undefined, language: 'Désactiver', selected: true },394 { id: 3, title: 'FRF', language: 'Français', selected: false },395 { id: 4, title: 'FR', language: 'Français', selected: false },396 { id: 5, title: 'EN', language: 'Anglais', selected: false }397 ]398 testTracks6 = [399 { id: -1, title: undefined, language: 'Disable', selected: false },400 { id: 0, title: undefined, language: 'My Awesome Movie (2017) VFF-ENG AC3 BluRay 1080p x264.GHT', selected: true }401 ]402 testTracks7 = [403 { id: -1, title: undefined, language: 'Désactiver', selected: false },404 { id: 1, title: 'VFF AC3 5.1 @448kbps', language: 'Français', selected: true },405 { id: 2, title: 'ENG AC3 5.1 @448kbps', language: 'Anglais', selected: false }406 ]407 testTracks8 = [408 { id: -1, title: undefined, language: 'Désactiver', selected: false },409 { id: 3, title: 'FR Forced', language: 'Français', selected: false },410 { id: 4, title: 'FR Forced Colored', language: 'Français', selected: true },411 { id: 5, title: 'FR Full', language: 'Français', selected: false },412 { id: 6, title: 'FR Full SDH', language: 'Français', selected: false },413 { id: 7, title: 'ENG Full', language: 'Anglais', selected: false },414 { id: 8, title: 'ENG Full SDH', language: 'Anglais', selected: false }415 ]416 })417 describe('Video track tests', () => {418 it ('Should get empty video tracks', async function () {419 let promise = vlcPlayer.getVideoTracks()420 fakeProcess.stdout.push("+----[ video-es ]\r\n")421 fakeProcess.stdout.push("+----[ end of video-es ]\r\n")422 fakeProcess.stdout.push("> ")423 let result = await promise424 expectedContext.setVideoTracks(emptyTracks)425 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())426 })427 it ('Should get default video tracks', async function () {428 let promise = vlcPlayer.getVideoTracks()429 fakeProcess.stdout.push("+----[ video-es ]\r\n")430 fakeProcess.stdout.push("| -1 - Disable\r\n")431 fakeProcess.stdout.push("| 0 - Track 1 *\r\n")432 fakeProcess.stdout.push("+----[ end of video-es ]\r\n")433 fakeProcess.stdout.push("> ")434 let result = await promise435 expectedContext.setVideoTracks(defaultTracks)436 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())437 })438 it ('Should get complex video tracks #1', async function () {439 let promise = vlcPlayer.getVideoTracks()440 fakeProcess.stdout.push("+----[ video-es ]\r\n")441 fakeProcess.stdout.push("| -1 - Désactiver\r\n")442 fakeProcess.stdout.push("| 1 - E-AC3 5.1 @ 640 Kbps - [Anglais] *\r\n")443 fakeProcess.stdout.push("| 2 - E-AC3 5.1 @ 640 Kbps - [Français]\r\n")444 fakeProcess.stdout.push("+----[ end of video-es ]\r\n")445 fakeProcess.stdout.push("> ")446 let result = await promise447 expectedContext.setVideoTracks(testTracks1)448 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())449 })450 it ('Should get complex video tracks #2', async function () {451 let promise = vlcPlayer.getVideoTracks()452 fakeProcess.stdout.push("+----[ video-es ]\r\n")453 fakeProcess.stdout.push("| -1 - Désactiver\r\n")454 fakeProcess.stdout.push("| 3 - Z1 (srt) - [Français] *\r\n")455 fakeProcess.stdout.push("| 4 - Sous-titrage malentendants 1\r\n")456 fakeProcess.stdout.push("| 5 - Sous-titrage malentendants 2\r\n")457 fakeProcess.stdout.push("| 6 - Sous-titrage malentendants 3\r\n")458 fakeProcess.stdout.push("| 7 - Sous-titrage malentendants 4\r\n")459 fakeProcess.stdout.push("+----[ end of video-es ]\r\n")460 fakeProcess.stdout.push("> ")461 let result = await promise462 expectedContext.setVideoTracks(testTracks2)463 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())464 })465 it ('Should get complex video tracks #3', async function () {466 let promise = vlcPlayer.getVideoTracks()467 fakeProcess.stdout.push("+----[ video-es ]\r\n")468 fakeProcess.stdout.push("| -1 - Désactiver\r\n")469 fakeProcess.stdout.push("| 0 - Piste 1 - [Anglais] *\r\n")470 fakeProcess.stdout.push("+----[ end of video-es ]\r\n")471 fakeProcess.stdout.push("> ")472 let result = await promise473 expectedContext.setVideoTracks(testTracks3)474 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())475 })476 it ('Should get complex video tracks #4', async function () {477 let promise = vlcPlayer.getVideoTracks()478 fakeProcess.stdout.push("+----[ video-es ]\r\n")479 fakeProcess.stdout.push("| -1 - Désactiver\r\n")480 fakeProcess.stdout.push("| 1 - FR - [Français] *\r\n")481 fakeProcess.stdout.push("| 2 - EN - [Anglais]\r\n")482 fakeProcess.stdout.push("+----[ end of video-es ]\r\n")483 fakeProcess.stdout.push("> ")484 let result = await promise485 expectedContext.setVideoTracks(testTracks4)486 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())487 })488 it ('Should get complex video tracks #5', async function () {489 let promise = vlcPlayer.getVideoTracks()490 fakeProcess.stdout.push("+----[ video-es ]\r\n")491 fakeProcess.stdout.push("| -1 - Désactiver *\r\n")492 fakeProcess.stdout.push("| 3 - FRF - [Français]\r\n")493 fakeProcess.stdout.push("| 4 - FR - [Français]\r\n")494 fakeProcess.stdout.push("| 5 - EN - [Anglais]\r\n")495 fakeProcess.stdout.push("+----[ end of video-es ]\r\n")496 fakeProcess.stdout.push("> ")497 let result = await promise498 expectedContext.setVideoTracks(testTracks5)499 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())500 })501 it ('Should get complex video tracks #6', async function () {502 let promise = vlcPlayer.getVideoTracks()503 fakeProcess.stdout.push("+----[ video-es ]\r\n")504 fakeProcess.stdout.push("| -1 - Disable\r\n")505 fakeProcess.stdout.push("| 0 - My Awesome Movie (2017) VFF-ENG AC3 BluRay 1080p x264.GHT *\r\n")506 fakeProcess.stdout.push("+----[ end of video-es ]\r\n")507 fakeProcess.stdout.push("> ")508 let result = await promise509 expectedContext.setVideoTracks(testTracks6)510 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())511 })512 it ('Should get complex video tracks #7', async function () {513 let promise = vlcPlayer.getVideoTracks()514 fakeProcess.stdout.push("+----[ video-es ]\r\n")515 fakeProcess.stdout.push("| -1 - Désactiver\r\n")516 fakeProcess.stdout.push("| 1 - VFF AC3 5.1 @448kbps - [Français] *\r\n")517 fakeProcess.stdout.push("| 2 - ENG AC3 5.1 @448kbps - [Anglais]\r\n")518 fakeProcess.stdout.push("+----[ end of video-es ]\r\n")519 fakeProcess.stdout.push("> ")520 let result = await promise521 expectedContext.setVideoTracks(testTracks7)522 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())523 })524 it ('Should get complex video tracks #8', async function () {525 let promise = vlcPlayer.getVideoTracks()526 fakeProcess.stdout.push("+----[ video-es ]\r\n")527 fakeProcess.stdout.push("| -1 - Désactiver\r\n")528 fakeProcess.stdout.push("| 3 - FR Forced - [Français]\r\n")529 fakeProcess.stdout.push("| 4 - FR Forced Colored - [Français] *\r\n")530 fakeProcess.stdout.push("| 5 - FR Full - [Français]\r\n")531 fakeProcess.stdout.push("| 6 - FR Full SDH - [Français]\r\n")532 fakeProcess.stdout.push("| 7 - ENG Full - [Anglais]\r\n")533 fakeProcess.stdout.push("| 8 - ENG Full SDH - [Anglais]\r\n")534 fakeProcess.stdout.push("+----[ end of video-es ]\r\n")535 fakeProcess.stdout.push("> ")536 let result = await promise537 expectedContext.setVideoTracks(testTracks8)538 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())539 })540 it ('Should update the selected video tracks', async function () {541 let promise = vlcPlayer.setVideoTrack(-1)542 fakeProcess.stdout.push("> ")543 let result = await promise544 expectedContext.setSelectedVideoTrack(-1)545 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())546 })547 it ('Should do nothing if the selected video track does not exist', async function () {548 /* This promise should be fulfilled immediately */549 let result = await vlcPlayer.setVideoTrack(-5)550 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())551 })552 })553 describe('Audio track tests', () => {554 it ('Should get empty audio tracks', async function () {555 let promise = vlcPlayer.getAudioTracks()556 fakeProcess.stdout.push("+----[ audio-es ]\r\n")557 fakeProcess.stdout.push("+----[ end of audio-es ]\r\n")558 fakeProcess.stdout.push("> ")559 let result = await promise560 expectedContext.setAudioTracks(emptyTracks)561 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())562 })563 it ('Should get default audio tracks', async function () {564 let promise = vlcPlayer.getAudioTracks()565 fakeProcess.stdout.push("+----[ audio-es ]\r\n")566 fakeProcess.stdout.push("| -1 - Disable\r\n")567 fakeProcess.stdout.push("| 0 - Track 1 *\r\n")568 fakeProcess.stdout.push("+----[ end of audio-es ]\r\n")569 fakeProcess.stdout.push("> ")570 let result = await promise571 expectedContext.setAudioTracks(defaultTracks)572 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())573 })574 it ('Should get complex audio tracks #1', async function () {575 let promise = vlcPlayer.getAudioTracks()576 fakeProcess.stdout.push("+----[ audio-es ]\r\n")577 fakeProcess.stdout.push("| -1 - Désactiver\r\n")578 fakeProcess.stdout.push("| 1 - E-AC3 5.1 @ 640 Kbps - [Anglais] *\r\n")579 fakeProcess.stdout.push("| 2 - E-AC3 5.1 @ 640 Kbps - [Français]\r\n")580 fakeProcess.stdout.push("+----[ end of audio-es ]\r\n")581 fakeProcess.stdout.push("> ")582 let result = await promise583 expectedContext.setAudioTracks(testTracks1)584 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())585 })586 it ('Should get complex audio tracks #2', async function () {587 let promise = vlcPlayer.getAudioTracks()588 fakeProcess.stdout.push("+----[ audio-es ]\r\n")589 fakeProcess.stdout.push("| -1 - Désactiver\r\n")590 fakeProcess.stdout.push("| 3 - Z1 (srt) - [Français] *\r\n")591 fakeProcess.stdout.push("| 4 - Sous-titrage malentendants 1\r\n")592 fakeProcess.stdout.push("| 5 - Sous-titrage malentendants 2\r\n")593 fakeProcess.stdout.push("| 6 - Sous-titrage malentendants 3\r\n")594 fakeProcess.stdout.push("| 7 - Sous-titrage malentendants 4\r\n")595 fakeProcess.stdout.push("+----[ end of audio-es ]\r\n")596 fakeProcess.stdout.push("> ")597 let result = await promise598 expectedContext.setAudioTracks(testTracks2)599 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())600 })601 it ('Should get complex audio tracks #3', async function () {602 let promise = vlcPlayer.getAudioTracks()603 fakeProcess.stdout.push("+----[ audio-es ]\r\n")604 fakeProcess.stdout.push("| -1 - Désactiver\r\n")605 fakeProcess.stdout.push("| 0 - Piste 1 - [Anglais] *\r\n")606 fakeProcess.stdout.push("+----[ end of audio-es ]\r\n")607 fakeProcess.stdout.push("> ")608 let result = await promise609 expectedContext.setAudioTracks(testTracks3)610 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())611 })612 it ('Should get complex audio tracks #4', async function () {613 let promise = vlcPlayer.getAudioTracks()614 fakeProcess.stdout.push("+----[ audio-es ]\r\n")615 fakeProcess.stdout.push("| -1 - Désactiver\r\n")616 fakeProcess.stdout.push("| 1 - FR - [Français] *\r\n")617 fakeProcess.stdout.push("| 2 - EN - [Anglais]\r\n")618 fakeProcess.stdout.push("+----[ end of audio-es ]\r\n")619 fakeProcess.stdout.push("> ")620 let result = await promise621 expectedContext.setAudioTracks(testTracks4)622 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())623 })624 it ('Should get complex audio tracks #5', async function () {625 let promise = vlcPlayer.getAudioTracks()626 fakeProcess.stdout.push("+----[ audio-es ]\r\n")627 fakeProcess.stdout.push("| -1 - Désactiver *\r\n")628 fakeProcess.stdout.push("| 3 - FRF - [Français]\r\n")629 fakeProcess.stdout.push("| 4 - FR - [Français]\r\n")630 fakeProcess.stdout.push("| 5 - EN - [Anglais]\r\n")631 fakeProcess.stdout.push("+----[ end of audio-es ]\r\n")632 fakeProcess.stdout.push("> ")633 let result = await promise634 expectedContext.setAudioTracks(testTracks5)635 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())636 })637 it ('Should get complex audio tracks #6', async function () {638 let promise = vlcPlayer.getAudioTracks()639 fakeProcess.stdout.push("+----[ audio-es ]\r\n")640 fakeProcess.stdout.push("| -1 - Disable\r\n")641 fakeProcess.stdout.push("| 0 - My Awesome Movie (2017) VFF-ENG AC3 BluRay 1080p x264.GHT *\r\n")642 fakeProcess.stdout.push("+----[ end of audio-es ]\r\n")643 fakeProcess.stdout.push("> ")644 let result = await promise645 expectedContext.setAudioTracks(testTracks6)646 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())647 })648 it ('Should get complex audio tracks #7', async function () {649 let promise = vlcPlayer.getAudioTracks()650 fakeProcess.stdout.push("+----[ audio-es ]\r\n")651 fakeProcess.stdout.push("| -1 - Désactiver\r\n")652 fakeProcess.stdout.push("| 1 - VFF AC3 5.1 @448kbps - [Français] *\r\n")653 fakeProcess.stdout.push("| 2 - ENG AC3 5.1 @448kbps - [Anglais]\r\n")654 fakeProcess.stdout.push("+----[ end of audio-es ]\r\n")655 fakeProcess.stdout.push("> ")656 let result = await promise657 expectedContext.setAudioTracks(testTracks7)658 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())659 })660 it ('Should get complex audio tracks #8', async function () {661 let promise = vlcPlayer.getAudioTracks()662 fakeProcess.stdout.push("+----[ audio-es ]\r\n")663 fakeProcess.stdout.push("| -1 - Désactiver\r\n")664 fakeProcess.stdout.push("| 3 - FR Forced - [Français]\r\n")665 fakeProcess.stdout.push("| 4 - FR Forced Colored - [Français] *\r\n")666 fakeProcess.stdout.push("| 5 - FR Full - [Français]\r\n")667 fakeProcess.stdout.push("| 6 - FR Full SDH - [Français]\r\n")668 fakeProcess.stdout.push("| 7 - ENG Full - [Anglais]\r\n")669 fakeProcess.stdout.push("| 8 - ENG Full SDH - [Anglais]\r\n")670 fakeProcess.stdout.push("+----[ end of audio-es ]\r\n")671 fakeProcess.stdout.push("> ")672 let result = await promise673 expectedContext.setAudioTracks(testTracks8)674 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())675 })676 it ('Should update the selected audio tracks', async function () {677 let promise = vlcPlayer.setAudioTrack(-1)678 fakeProcess.stdout.push("> ")679 let result = await promise680 expectedContext.setSelectedAudioTrack(-1)681 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())682 })683 it ('Should do nothing if the selected audio track does not exist', async function () {684 /* This promise should be fulfilled immediately */685 let result = await vlcPlayer.setAudioTrack(-5)686 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())687 })688 })689 describe('Subtitle track tests', () => {690 it ('Should get empty subtitle tracks', async function () {691 let promise = vlcPlayer.getSubtitleTracks()692 fakeProcess.stdout.push("+----[ spu-es ]\r\n")693 fakeProcess.stdout.push("+----[ end of spu-es ]\r\n")694 fakeProcess.stdout.push("> ")695 let result = await promise696 expectedContext.setSubtitleTracks(emptyTracks)697 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())698 })699 it ('Should get default subtitle tracks', async function () {700 let promise = vlcPlayer.getSubtitleTracks()701 fakeProcess.stdout.push("+----[ spu-es ]\r\n")702 fakeProcess.stdout.push("| -1 - Disable\r\n")703 fakeProcess.stdout.push("| 0 - Track 1 *\r\n")704 fakeProcess.stdout.push("+----[ end of spu-es ]\r\n")705 fakeProcess.stdout.push("> ")706 let result = await promise707 expectedContext.setSubtitleTracks(defaultTracks)708 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())709 })710 it ('Should get complex subtitle tracks #1', async function () {711 let promise = vlcPlayer.getSubtitleTracks()712 fakeProcess.stdout.push("+----[ spu-es ]\r\n")713 fakeProcess.stdout.push("| -1 - Désactiver\r\n")714 fakeProcess.stdout.push("| 1 - E-AC3 5.1 @ 640 Kbps - [Anglais] *\r\n")715 fakeProcess.stdout.push("| 2 - E-AC3 5.1 @ 640 Kbps - [Français]\r\n")716 fakeProcess.stdout.push("+----[ end of spu-es ]\r\n")717 fakeProcess.stdout.push("> ")718 let result = await promise719 expectedContext.setSubtitleTracks(testTracks1)720 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())721 })722 it ('Should get complex subtitle tracks #2', async function () {723 let promise = vlcPlayer.getSubtitleTracks()724 fakeProcess.stdout.push("+----[ spu-es ]\r\n")725 fakeProcess.stdout.push("| -1 - Désactiver\r\n")726 fakeProcess.stdout.push("| 3 - Z1 (srt) - [Français] *\r\n")727 fakeProcess.stdout.push("| 4 - Sous-titrage malentendants 1\r\n")728 fakeProcess.stdout.push("| 5 - Sous-titrage malentendants 2\r\n")729 fakeProcess.stdout.push("| 6 - Sous-titrage malentendants 3\r\n")730 fakeProcess.stdout.push("| 7 - Sous-titrage malentendants 4\r\n")731 fakeProcess.stdout.push("+----[ end of spu-es ]\r\n")732 fakeProcess.stdout.push("> ")733 let result = await promise734 expectedContext.setSubtitleTracks(testTracks2)735 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())736 })737 it ('Should get complex subtitle tracks #3', async function () {738 let promise = vlcPlayer.getSubtitleTracks()739 fakeProcess.stdout.push("+----[ spu-es ]\r\n")740 fakeProcess.stdout.push("| -1 - Désactiver\r\n")741 fakeProcess.stdout.push("| 0 - Piste 1 - [Anglais] *\r\n")742 fakeProcess.stdout.push("+----[ end of spu-es ]\r\n")743 fakeProcess.stdout.push("> ")744 let result = await promise745 expectedContext.setSubtitleTracks(testTracks3)746 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())747 })748 it ('Should get complex subtitle tracks #4', async function () {749 let promise = vlcPlayer.getSubtitleTracks()750 fakeProcess.stdout.push("+----[ spu-es ]\r\n")751 fakeProcess.stdout.push("| -1 - Désactiver\r\n")752 fakeProcess.stdout.push("| 1 - FR - [Français] *\r\n")753 fakeProcess.stdout.push("| 2 - EN - [Anglais]\r\n")754 fakeProcess.stdout.push("+----[ end of spu-es ]\r\n")755 fakeProcess.stdout.push("> ")756 let result = await promise757 expectedContext.setSubtitleTracks(testTracks4)758 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())759 })760 it ('Should get complex subtitle tracks #5', async function () {761 let promise = vlcPlayer.getSubtitleTracks()762 fakeProcess.stdout.push("+----[ spu-es ]\r\n")763 fakeProcess.stdout.push("| -1 - Désactiver *\r\n")764 fakeProcess.stdout.push("| 3 - FRF - [Français]\r\n")765 fakeProcess.stdout.push("| 4 - FR - [Français]\r\n")766 fakeProcess.stdout.push("| 5 - EN - [Anglais]\r\n")767 fakeProcess.stdout.push("+----[ end of spu-es ]\r\n")768 fakeProcess.stdout.push("> ")769 let result = await promise770 expectedContext.setSubtitleTracks(testTracks5)771 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())772 })773 it ('Should get complex subtitle tracks #6', async function () {774 let promise = vlcPlayer.getSubtitleTracks()775 fakeProcess.stdout.push("+----[ spu-es ]\r\n")776 fakeProcess.stdout.push("| -1 - Disable\r\n")777 fakeProcess.stdout.push("| 0 - My Awesome Movie (2017) VFF-ENG AC3 BluRay 1080p x264.GHT *\r\n")778 fakeProcess.stdout.push("+----[ end of spu-es ]\r\n")779 fakeProcess.stdout.push("> ")780 let result = await promise781 expectedContext.setSubtitleTracks(testTracks6)782 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())783 })784 it ('Should get complex subtitle tracks #7', async function () {785 let promise = vlcPlayer.getSubtitleTracks()786 fakeProcess.stdout.push("+----[ spu-es ]\r\n")787 fakeProcess.stdout.push("| -1 - Désactiver\r\n")788 fakeProcess.stdout.push("| 1 - VFF AC3 5.1 @448kbps - [Français] *\r\n")789 fakeProcess.stdout.push("| 2 - ENG AC3 5.1 @448kbps - [Anglais]\r\n")790 fakeProcess.stdout.push("+----[ end of spu-es ]\r\n")791 fakeProcess.stdout.push("> ")792 let result = await promise793 expectedContext.setSubtitleTracks(testTracks7)794 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())795 })796 it ('Should get complex subtitle tracks #8', async function () {797 let promise = vlcPlayer.getSubtitleTracks()798 fakeProcess.stdout.push("+----[ spu-es ]\r\n")799 fakeProcess.stdout.push("| -1 - Désactiver\r\n")800 fakeProcess.stdout.push("| 3 - FR Forced - [Français]\r\n")801 fakeProcess.stdout.push("| 4 - FR Forced Colored - [Français] *\r\n")802 fakeProcess.stdout.push("| 5 - FR Full - [Français]\r\n")803 fakeProcess.stdout.push("| 6 - FR Full SDH - [Français]\r\n")804 fakeProcess.stdout.push("| 7 - ENG Full - [Anglais]\r\n")805 fakeProcess.stdout.push("| 8 - ENG Full SDH - [Anglais]\r\n")806 fakeProcess.stdout.push("+----[ end of spu-es ]\r\n")807 fakeProcess.stdout.push("> ")808 let result = await promise809 expectedContext.setSubtitleTracks(testTracks8)810 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())811 })812 it ('Should update the selected subtitle tracks', async function () {813 let promise = vlcPlayer.setSubtitleTrack(-1)814 fakeProcess.stdout.push("> ")815 let result = await promise816 expectedContext.setSelectedSubtitleTrack(-1)817 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())818 })819 it ('Should do nothing if the selected subtitle track does not exist', async function () {820 /* This promise should be fulfilled immediately */821 let result = await vlcPlayer.setSubtitleTrack(-5)822 expect(result).to.deep.equal(expectedContext.toFormattedPlayerData())823 })824 })825 })...

Full Screen

Full Screen

CanvasUtils.test.js

Source:CanvasUtils.test.js Github

copy

Full Screen

1/* eslint-disable func-names */2import pixelmatch from 'pixelmatch';3import fs from 'fs';4import { List } from 'immutable';5import { ContentState, convertFromHTML, ContentBlock, genKey } from 'draft-js';6import { createImageData } from 'canvas';7import { exampleText, writeText2 } from './CanvasUtils';8// const imgOutputFolder = `${__dirname}/output`;9// To get test name in Jasmine10/*11let testName = '';12jasmine.getEnv().addReporter({13 specStarted: (result) => {14 testName = result.fullName;15 }16});17*/18export const logJSON = (json) => {19 console.log(JSON.stringify(json, null, 2));20};21const getPixelDifference = (expectedContext, actualContext, diffContext, canvasWidth, canvasHeight) => {22 const expectedImage = expectedContext.getImageData(0, 0, canvasWidth, canvasHeight);23 const actualImage = actualContext.getImageData(0, 0, canvasWidth, canvasHeight);24 const diffImage = createImageData(canvasWidth, canvasHeight);25 const pixelDiff = pixelmatch(expectedImage.data, actualImage.data, diffImage.data, canvasWidth, canvasHeight);26 diffContext.putImageData(diffImage, 0, 0);27 return pixelDiff;28};29export const outputCanvasAsPNG = (canvas, filePath) => {30 const data = canvas.toDataURL().replace(/^data:image\/\w+;base64,/, '');31 const buffer = Buffer.from(data, 'base64');32 fs.writeFile(filePath, buffer, (err) => {33 if (err) return console.log(err);34 return 0;35 });36};37const createEmptyBlock = () =>38 new ContentBlock({39 key: genKey(),40 type: 'unstyled',41 text: '',42 characterList: List(),43 depth: 0,44 data: {},45 });46const getDraftJSContentFromHTML = (html) => {47 const blocksFromHTML = convertFromHTML(html);48 return ContentState.createFromBlockArray(blocksFromHTML.contentBlocks, blocksFromHTML.entityMap);49};50describe('CanvasUtils', () => {51 const expectedCanvas = document.createElement('canvas');52 const actualCanvas = document.createElement('canvas');53 const diffCanvas = document.createElement('canvas');54 const canvasWidth = 250;55 const canvasHeight = 100;56 const fontSize = 20;57 const lineHeight = fontSize;58 const paragraphSpace = 5;59 const font = 'sans-serif';60 const textBaseline = 'bottom';61 const textOptions = {62 width: canvasWidth,63 height: canvasHeight,64 font,65 fontSize,66 fontSizeInPx: fontSize,67 };68 const expectedContext = expectedCanvas.getContext('2d');69 const actualContext = actualCanvas.getContext('2d');70 const diffContext = diffCanvas.getContext('2d');71 beforeAll(() => {72 expectedCanvas.width = canvasWidth;73 expectedCanvas.height = canvasHeight;74 actualCanvas.width = canvasWidth;75 actualCanvas.height = canvasHeight;76 diffCanvas.width = canvasWidth;77 diffCanvas.height = canvasHeight;78 expectedContext.font = `${fontSize}px ${font}`;79 expectedContext.textBaseline = textBaseline;80 actualContext.font = `${fontSize}px ${font}`;81 actualContext.textBaseline = textBaseline;82 diffContext.font = `${fontSize}px ${font}`;83 diffContext.textBaseline = textBaseline;84 });85 beforeEach(() => {86 expectedContext.clearRect(0, 0, canvasWidth, canvasHeight);87 actualContext.clearRect(0, 0, canvasWidth, canvasHeight);88 diffContext.clearRect(0, 0, canvasWidth, canvasHeight);89 });90 afterEach(function () {91 // Reset any applied styling92 expectedContext.font = `${fontSize}px ${font}`;93 expectedContext.textBaseline = textBaseline;94 actualContext.font = `${fontSize}px ${font}`;95 actualContext.textBaseline = textBaseline;96 diffContext.font = `${fontSize}px ${font}`;97 diffContext.textBaseline = textBaseline;98 /*99 // When required, output diff images100 const testName = `${expect.getState().currentTestName}`;101 outputCanvasAsPNG(diffCanvas, `${imgOutputFolder}/diff/${testName}.png`);102 // When required, output actual and expected images103 outputCanvasAsPNG(actualCanvas, `${imgOutputFolder}/actual/${testName}.png`);104 outputCanvasAsPNG(expectedCanvas, `${imgOutputFolder}/expected/${testName}.png`);105 */106 });107 it('exists', () => {108 expect(actualCanvas).not.toEqual(undefined);109 });110 it('passes similarity test', () => {111 expectedContext.fillText('expected', 0, lineHeight);112 actualContext.fillText('expected', 0, lineHeight);113 expect(getPixelDifference(expectedContext, actualContext, diffContext, canvasWidth, canvasHeight)).toEqual(0);114 });115 it('passes difference test', () => {116 expectedContext.fillText('expected', 0, lineHeight);117 actualContext.fillText('actual', 0, lineHeight);118 expect(getPixelDifference(expectedContext, actualContext, diffContext, canvasWidth, canvasHeight)).not.toEqual(0);119 });120 it('passes letter-by-letter test', () => {121 expectedContext.fillText('letter-by-letter', 0, lineHeight);122 const content = ContentState.createFromText('letter-by-letter');123 const { offscreens } = writeText2(actualCanvas, content, textOptions);124 expect(getPixelDifference(expectedContext, offscreens[0], diffContext, canvasWidth, canvasHeight)).toEqual(0);125 });126 it('passes bold style test', () => {127 expectedContext.font = `bold ${expectedContext.font}`;128 expectedContext.fillText('bold test', 0, lineHeight);129 const content = getDraftJSContentFromHTML('<b>bold test</b>');130 const { offscreens } = writeText2(actualCanvas, content, textOptions);131 expect(getPixelDifference(expectedContext, offscreens[0], diffContext, canvasWidth, canvasHeight)).toEqual(0);132 });133 it('passes italic style test', () => {134 expectedContext.font = `italic ${expectedContext.font}`;135 expectedContext.fillText('italic test', 0, lineHeight);136 const content = getDraftJSContentFromHTML('<i>italic test</i>');137 const { offscreens } = writeText2(actualCanvas, content, textOptions);138 expect(getPixelDifference(expectedContext, offscreens[0], diffContext, canvasWidth, canvasHeight)).toEqual(0);139 });140 it('passes bold italic style test', () => {141 expectedContext.font = `bold italic ${expectedContext.font}`;142 expectedContext.fillText('bold italic test', 0, lineHeight);143 const content = getDraftJSContentFromHTML('<b><i>bold italic test</i></b>');144 const { offscreens } = writeText2(actualCanvas, content, textOptions);145 expect(getPixelDifference(expectedContext, offscreens[0], diffContext, canvasWidth, canvasHeight)).toEqual(0);146 });147 it('passes style change test', () => {148 const defaultStyle = expectedContext.font;149 expectedContext.font = `bold ${defaultStyle}`;150 expectedContext.fillText('style', 0, lineHeight);151 let position = expectedContext.measureText('style').width;152 expectedContext.font = defaultStyle;153 expectedContext.fillText(' change ', position, lineHeight);154 position += expectedContext.measureText(' change ').width;155 expectedContext.font = `italic ${defaultStyle}`;156 expectedContext.fillText('test', position, lineHeight);157 const content = getDraftJSContentFromHTML('<b>style</b> change <i>test</i>');158 const { offscreens } = writeText2(actualCanvas, content, textOptions);159 expect(getPixelDifference(expectedContext, offscreens[0], diffContext, canvasWidth, canvasHeight)).toEqual(0);160 });161 it('passes text wrap test', () => {162 expectedContext.fillText('this is a multiline text', 0, lineHeight);163 expectedContext.fillText('that should be wrapped', 0, lineHeight + lineHeight);164 const content = getDraftJSContentFromHTML('this is a multiline text that should be wrapped');165 const { offscreens } = writeText2(actualCanvas, content, textOptions);166 expect(getPixelDifference(expectedContext, offscreens[0], diffContext, canvasWidth, canvasHeight)).toEqual(0);167 });168 it('passes multiple paragraph test', () => {169 expectedContext.fillText('paragraph1 text', 0, lineHeight);170 expectedContext.fillText('paragraph2 text', 0, lineHeight + lineHeight + paragraphSpace);171 const content = getDraftJSContentFromHTML('<p>paragraph1 text</p><p>paragraph2 text</p>');172 const { offscreens } = writeText2(actualCanvas, content, textOptions);173 expect(getPixelDifference(expectedContext, offscreens[0], diffContext, canvasWidth, canvasHeight)).toEqual(0);174 });175 it('passes paragraph wrap test', () => {176 expectedContext.fillText('paragraph text should', 0, lineHeight);177 expectedContext.fillText('be wrapped', 0, lineHeight + lineHeight);178 const content = getDraftJSContentFromHTML('<p>paragraph text should be wrapped</p>');179 const { offscreens } = writeText2(actualCanvas, content, textOptions);180 expect(getPixelDifference(expectedContext, offscreens[0], diffContext, canvasWidth, canvasHeight)).toEqual(0);181 });182 it('passes two paragraph wrap test', () => {183 expectedContext.fillText('paragraph text should', 0, lineHeight);184 expectedContext.fillText('be wrapped', 0, lineHeight + lineHeight);185 expectedContext.fillText('paragraph', 0, lineHeight + lineHeight + lineHeight + paragraphSpace);186 const content = getDraftJSContentFromHTML('<p>paragraph text should be wrapped</p><p>paragraph</p>');187 const { offscreens } = writeText2(actualCanvas, content, textOptions);188 expect(getPixelDifference(expectedContext, offscreens[0], diffContext, canvasWidth, canvasHeight)).toEqual(0);189 });190 it('passes empty paragraph test', () => {191 expectedContext.fillText('Second line', 0, lineHeight + paragraphSpace + lineHeight);192 const emptyBlock = [createEmptyBlock()];193 const secondBlock = convertFromHTML('<p>Second line</p>').contentBlocks;194 const mergedBlocks = emptyBlock.concat(secondBlock);195 const content = ContentState.createFromBlockArray(mergedBlocks, {});196 const { offscreens } = writeText2(actualCanvas, content, textOptions);197 expect(getPixelDifference(expectedContext, offscreens[0], diffContext, canvasWidth, canvasHeight)).toEqual(0);198 });199 it('passes two empty paragraph test', () => {200 expectedContext.fillText('Third line', 0, lineHeight + lineHeight + lineHeight + paragraphSpace);201 const emptyBlocks = [createEmptyBlock(), createEmptyBlock()];202 const thirdBlock = convertFromHTML('<p>Third line</p>').contentBlocks;203 const mergedBlocks = emptyBlocks.concat(thirdBlock);204 const content = ContentState.createFromBlockArray(mergedBlocks, {});205 const { offscreens } = writeText2(actualCanvas, content, textOptions);206 expect(getPixelDifference(expectedContext, offscreens[0], diffContext, canvasWidth, canvasHeight)).toEqual(0);207 });208 it('passes empty paragraph between test', () => {209 expectedContext.fillText('First line', 0, lineHeight);210 expectedContext.fillText('Third line', 0, lineHeight + lineHeight + lineHeight + paragraphSpace);211 const firstBlock = convertFromHTML('<p>First line</p>').contentBlocks;212 const emptyBlock = [createEmptyBlock()];213 const thirdBlock = convertFromHTML('<p>Third line</p>').contentBlocks;214 const mergedBlocks = firstBlock.concat(emptyBlock).concat(thirdBlock);215 const content = ContentState.createFromBlockArray(mergedBlocks, {});216 const { offscreens } = writeText2(actualCanvas, content, textOptions);217 expect(getPixelDifference(expectedContext, offscreens[0], diffContext, canvasWidth, canvasHeight)).toEqual(0);218 });219 it('passes paragraph extra spaces test', () => {220 expectedContext.fillText('Paragraph', 0, lineHeight);221 expectedContext.fillText('with space', 0, lineHeight + lineHeight + paragraphSpace);222 const content = getDraftJSContentFromHTML('<p>Paragraph </p><p>with space</p>');223 const { offscreens } = writeText2(actualCanvas, content, textOptions);224 expect(getPixelDifference(expectedContext, offscreens[0], diffContext, canvasWidth, canvasHeight)).toEqual(0);225 });226 it('resizes canvas to fit text', () => {227 expectedContext.fillText('Visible', 0, lineHeight);228 expectedContext.fillText('Overflow', 0, lineHeight + lineHeight + paragraphSpace);229 const content = getDraftJSContentFromHTML('<p>Visible</p><p>Overflow</p>');230 const { offscreens } = writeText2(actualCanvas, content, textOptions);231 expect(getPixelDifference(expectedContext, offscreens[0], diffContext, canvasWidth, canvasHeight)).toEqual(0);232 });233 it('returns single word metadata', () => {234 const content = getDraftJSContentFromHTML('metadata');235 const singleWordWidth = expectedContext.measureText('metadata').width;236 const expectedWordsMetadata = [237 {238 word: 'metadata',239 lineNumber: 0,240 rect: {241 top: 0,242 right: singleWordWidth,243 bottom: lineHeight,244 left: 0,245 },246 },247 ];248 const actualTextMetadata = writeText2(actualCanvas, content, textOptions, { paragraphSpace });249 expect(actualTextMetadata.wordsMetadata).toEqual(expectedWordsMetadata);250 expect(actualTextMetadata.linesMetadata.length).toEqual(1);251 expect(actualTextMetadata.pagesMetadata.length).toEqual(1);252 });253 it('returns multiple words metadata', () => {254 const content = getDraftJSContentFromHTML('multiple word metadata');255 const firstWordWidth = expectedContext.measureText('multiple').width;256 const secondWordWidth = expectedContext.measureText('word').width;257 const thirdWordWidth = expectedContext.measureText('metadata').width;258 const spaceWidth = expectedContext.measureText(' ').width;259 const secondWordStart = firstWordWidth + spaceWidth;260 const thirdWordStart = secondWordStart + secondWordWidth + spaceWidth;261 const expectedWordsMetadata = [262 {263 word: 'multiple',264 lineNumber: 0,265 rect: {266 top: 0,267 right: firstWordWidth,268 bottom: lineHeight,269 left: 0,270 },271 },272 {273 word: 'word',274 lineNumber: 0,275 rect: {276 top: 0,277 right: secondWordStart + secondWordWidth,278 bottom: lineHeight,279 left: secondWordStart,280 },281 },282 {283 word: 'metadata',284 lineNumber: 0,285 rect: {286 top: 0,287 right: thirdWordStart + thirdWordWidth,288 bottom: lineHeight,289 left: thirdWordStart,290 },291 },292 ];293 const actualTextMetadata = writeText2(actualCanvas, content, textOptions, { paragraphSpace });294 expect(actualTextMetadata.wordsMetadata).toEqual(expectedWordsMetadata);295 expect(actualTextMetadata.linesMetadata.length).toEqual(1);296 expect(actualTextMetadata.pagesMetadata.length).toEqual(1);297 });298 it('returns multiple line metadata', () => {299 const content = getDraftJSContentFromHTML('multiple word metadata that should be wrapped');300 const firstWordWidth = expectedContext.measureText('that').width;301 const secondWordWidth = expectedContext.measureText('should').width;302 const thirdWordWidth = expectedContext.measureText('be').width;303 const fourthWordWidth = expectedContext.measureText('wrapped').width;304 const spaceWidth = expectedContext.measureText(' ').width;305 const secondWordStart = firstWordWidth + spaceWidth;306 const thirdWordStart = secondWordStart + secondWordWidth + spaceWidth;307 const fourthWordStart = thirdWordStart + thirdWordWidth + spaceWidth;308 const expectedWordsMetadata = [309 {310 word: 'that',311 lineNumber: 1,312 rect: {313 top: lineHeight,314 right: firstWordWidth,315 bottom: lineHeight + lineHeight,316 left: 0,317 },318 },319 {320 word: 'should',321 lineNumber: 1,322 rect: {323 top: lineHeight,324 right: secondWordStart + secondWordWidth,325 bottom: lineHeight + lineHeight,326 left: secondWordStart,327 },328 },329 {330 word: 'be',331 lineNumber: 1,332 rect: {333 top: lineHeight,334 right: thirdWordStart + thirdWordWidth,335 bottom: lineHeight + lineHeight,336 left: thirdWordStart,337 },338 },339 {340 word: 'wrapped',341 lineNumber: 1,342 rect: {343 top: lineHeight,344 right: fourthWordStart + fourthWordWidth,345 bottom: lineHeight + lineHeight,346 left: fourthWordStart,347 },348 },349 ];350 const actualTextMetadata = writeText2(actualCanvas, content, textOptions, { paragraphSpace });351 expect(actualTextMetadata.wordsMetadata).toEqual(expect.arrayContaining(expectedWordsMetadata));352 expect(actualTextMetadata.linesMetadata.length).toEqual(2);353 expect(actualTextMetadata.pagesMetadata.length).toEqual(1);354 });355 it('returns multiple paragraph metadata', () => {356 const content = getDraftJSContentFromHTML('<p>paragraph1 text</p><p>paragraph2 text</p>');357 const firstWordWidth = expectedContext.measureText('paragraph1').width;358 const secondWordWidth = expectedContext.measureText('text').width;359 const spaceWidth = expectedContext.measureText(' ').width;360 const secondWordStart = firstWordWidth + spaceWidth;361 const expectedWordsMetadata = [362 {363 word: 'paragraph1',364 lineNumber: 0,365 rect: {366 top: 0,367 right: firstWordWidth,368 bottom: lineHeight,369 left: 0,370 },371 },372 {373 word: 'text',374 lineNumber: 0,375 rect: {376 top: 0,377 right: secondWordStart + secondWordWidth,378 bottom: lineHeight,379 left: secondWordStart,380 },381 },382 {383 word: 'paragraph2',384 lineNumber: 1,385 rect: {386 top: lineHeight + paragraphSpace,387 right: firstWordWidth,388 bottom: lineHeight + paragraphSpace + lineHeight,389 left: 0,390 },391 },392 {393 word: 'text',394 lineNumber: 1,395 rect: {396 top: lineHeight + paragraphSpace,397 right: secondWordStart + secondWordWidth,398 bottom: lineHeight + paragraphSpace + lineHeight,399 left: secondWordStart,400 },401 },402 ];403 const actualTextMetadata = writeText2(actualCanvas, content, textOptions, { paragraphSpace });404 expect(actualTextMetadata.wordsMetadata).toEqual(expectedWordsMetadata);405 expect(actualTextMetadata.linesMetadata.length).toEqual(2);406 expect(actualTextMetadata.pagesMetadata.length).toEqual(1);407 });408 it('returns paragraph wrap metadata', () => {409 const content = getDraftJSContentFromHTML('<p>paragraph text should be wrapped</p>');410 const lastWordWidth = expectedContext.measureText('be').width;411 const expectedWordsMetadata = {412 word: 'be',413 lineNumber: 1,414 rect: {415 top: lineHeight,416 right: lastWordWidth,417 bottom: lineHeight + lineHeight,418 left: 0,419 },420 };421 const actualTextMetadata = writeText2(actualCanvas, content, textOptions, { paragraphSpace });422 expect(actualTextMetadata.wordsMetadata[3]).toEqual(expectedWordsMetadata);423 expect(actualTextMetadata.linesMetadata.length).toEqual(2);424 expect(actualTextMetadata.pagesMetadata.length).toEqual(1);425 });426 it('returns empty paragraph metadata', () => {427 const emptyBlock = [createEmptyBlock()];428 const secondBlock = convertFromHTML('<p>Second</p>').contentBlocks;429 const mergedBlocks = emptyBlock.concat(secondBlock);430 const content = ContentState.createFromBlockArray(mergedBlocks, {});431 const firstWordWidth = expectedContext.measureText('Second').width;432 const expectedWordsMetadata = {433 word: 'Second',434 lineNumber: 0,435 rect: {436 top: lineHeight + paragraphSpace,437 right: firstWordWidth,438 bottom: lineHeight + paragraphSpace + lineHeight,439 left: 0,440 },441 };442 const actualTextMetadata = writeText2(actualCanvas, content, textOptions, { paragraphSpace });443 expect(actualTextMetadata.wordsMetadata[0]).toEqual(expectedWordsMetadata);444 expect(actualTextMetadata.linesMetadata.length).toEqual(1);445 expect(actualTextMetadata.pagesMetadata.length).toEqual(1);446 });447 it('returns two empty paragraph metadata', () => {448 const emptyBlocks = [createEmptyBlock(), createEmptyBlock()];449 const thirdBlock = convertFromHTML('<p>Third</p>').contentBlocks;450 const mergedBlocks = emptyBlocks.concat(thirdBlock);451 const content = ContentState.createFromBlockArray(mergedBlocks, {});452 const firstWordWidth = expectedContext.measureText('Third').width;453 const expectedWordsMetadata = {454 word: 'Third',455 lineNumber: 0,456 rect: {457 top: lineHeight + lineHeight + paragraphSpace,458 right: firstWordWidth,459 bottom: lineHeight + lineHeight + lineHeight + paragraphSpace,460 left: 0,461 },462 };463 const actualTextMetadata = writeText2(actualCanvas, content, textOptions, { paragraphSpace });464 expect(actualTextMetadata.wordsMetadata[0]).toEqual(expectedWordsMetadata);465 expect(actualTextMetadata.linesMetadata.length).toEqual(1);466 expect(actualTextMetadata.pagesMetadata.length).toEqual(1);467 });468 it('returns empty paragraph between metadata', () => {469 const firstBlock = convertFromHTML('<p>First</p>').contentBlocks;470 const emptyBlock = [createEmptyBlock()];471 const thirdBlock = convertFromHTML('<p>Third</p>').contentBlocks;472 const mergedBlocks = firstBlock.concat(emptyBlock).concat(thirdBlock);473 const content = ContentState.createFromBlockArray(mergedBlocks, {});474 const firstWordWidth = expectedContext.measureText('First').width;475 const thirdWordWidth = expectedContext.measureText('Third').width;476 const expectedWordsMetadata = [477 {478 word: 'First',479 lineNumber: 0,480 rect: {481 top: 0,482 right: firstWordWidth,483 bottom: lineHeight,484 left: 0,485 },486 },487 {488 word: 'Third',489 lineNumber: 1,490 rect: {491 top: lineHeight + lineHeight + paragraphSpace,492 right: thirdWordWidth,493 bottom: lineHeight + lineHeight + lineHeight + paragraphSpace,494 left: 0,495 },496 },497 ];498 const actualTextMetadata = writeText2(actualCanvas, content, textOptions, { paragraphSpace });499 expect(actualTextMetadata.wordsMetadata).toEqual(expectedWordsMetadata);500 expect(actualTextMetadata.linesMetadata.length).toEqual(2);501 expect(actualTextMetadata.pagesMetadata.length).toEqual(1);502 });503 it('returns two paragraph wrap metadata', () => {504 const content = getDraftJSContentFromHTML('<p>paragraph text should be wrapped</p><p>paragraph</p>');505 const wrappedWordWidth = expectedContext.measureText('be').width;506 const expectedWordsMetadata = {507 word: 'be',508 lineNumber: 1,509 rect: {510 top: lineHeight,511 right: wrappedWordWidth,512 bottom: lineHeight + lineHeight,513 left: 0,514 },515 };516 const actualTextMetadata = writeText2(actualCanvas, content, textOptions, { paragraphSpace });517 expect(actualTextMetadata.wordsMetadata[3]).toEqual(expectedWordsMetadata);518 expect(actualTextMetadata.pagesMetadata.length).toEqual(1);519 });520 it('returns text metadata', () => {521 const textMetadata = writeText2(actualCanvas, exampleText.contentState, textOptions, { paragraphSpace });522 expect(textMetadata.wordsMetadata.length).toEqual(exampleText.wordCount);523 expect(textMetadata.linesMetadata.length).toEqual(38);524 expect(textMetadata.pagesMetadata.length).toEqual(8);525 expect(textMetadata.offscreens.length).toEqual(8);526 });...

Full Screen

Full Screen

buildContext.test.ts

Source:buildContext.test.ts Github

copy

Full Screen

1/**2 * Tupaia3 * Copyright (c) 2017 - 2021 Beyond Essential Systems Pty Ltd4 */5import { AccessPolicy } from '@tupaia/access-policy';6import { Row } from '../../../reportBuilder';7import { buildContext, ReqContext } from '../../../reportBuilder/context/buildContext';8import { entityApiMock } from '../testUtils';9describe('buildContext', () => {10 const HIERARCHY = 'test_hierarchy';11 const ENTITIES = {12 test_hierarchy: [13 { id: 'ouId1', code: 'AU', name: 'Australia', type: 'country', attributes: {} },14 { id: 'ouId2', code: 'FJ', name: 'Fiji', type: 'country', attributes: {} },15 { id: 'ouId3', code: 'KI', name: 'Kiribati', type: 'country', attributes: {} },16 { id: 'ouId4', code: 'TO', name: 'Tonga', type: 'country', attributes: {} },17 {18 id: 'ouId5',19 code: 'TO_Facility1',20 name: 'Tonga Facility 1',21 type: 'facility',22 attributes: { x: 1 },23 },24 {25 id: 'ouId6',26 code: 'TO_Facility2',27 name: 'Tonga Facility 2',28 type: 'facility',29 attributes: {},30 },31 { id: 'ouId7', code: 'FJ_Facility', name: 'Fiji Facility', type: 'facility', attributes: {} },32 ],33 };34 const RELATIONS = {35 test_hierarchy: [36 { parent: 'TO', child: 'TO_Facility1' },37 { parent: 'TO', child: 'TO_Facility2' },38 { parent: 'FJ', child: 'FJ_Facility' },39 ],40 };41 const apiMock = entityApiMock(ENTITIES, RELATIONS);42 const reqContext: ReqContext = {43 hierarchy: HIERARCHY,44 permissionGroup: 'Public',45 services: {46 entity: apiMock,47 } as ReqContext['services'],48 accessPolicy: new AccessPolicy({ AU: ['Public'] }),49 };50 describe('orgUnits', () => {51 it('adds query to the context', async () => {52 const transform: unknown = [];53 const analytics: Row[] = [];54 const data = { results: analytics };55 const query = { test: 'yes' };56 const context = await buildContext(transform, reqContext, data, query);57 const expectedContext = {58 query,59 };60 expect(context).toStrictEqual(expectedContext);61 });62 it('builds orgUnits using fetched analytics', async () => {63 const transform = [64 {65 insert: {66 name: '=orgUnitCodeToName($organisationUnit)',67 },68 transform: 'updateColumns',69 },70 ];71 const analytics = [72 { dataElement: 'BCD1', organisationUnit: 'TO', period: '20210101', value: 1 },73 { dataElement: 'BCD1', organisationUnit: 'FJ', period: '20210101', value: 2 },74 ];75 const data = { results: analytics };76 const context = await buildContext(transform, reqContext, data);77 const expectedContext = {78 orgUnits: [79 { id: 'ouId2', code: 'FJ', name: 'Fiji', attributes: {} },80 { id: 'ouId4', code: 'TO', name: 'Tonga', attributes: {} },81 ],82 };83 expect(context).toStrictEqual(expectedContext);84 });85 it('builds orgUnits using fetched events', async () => {86 const transform = [87 {88 insert: {89 name: '=orgUnitCodeToName($orgUnit)',90 },91 transform: 'updateColumns',92 },93 ];94 const events = [95 { event: 'evId1', eventDate: '2021-01-01T12:00:00', orgUnit: 'TO', orgUnitName: 'Tonga' },96 { event: 'evId2', eventDate: '2021-01-01T12:00:00', orgUnit: 'FJ', orgUnitName: 'Fiji' },97 ];98 const data = { results: events };99 const context = await buildContext(transform, reqContext, data);100 const expectedContext = {101 orgUnits: [102 { id: 'ouId2', code: 'FJ', name: 'Fiji', attributes: {} },103 { id: 'ouId4', code: 'TO', name: 'Tonga', attributes: {} },104 ],105 };106 expect(context).toStrictEqual(expectedContext);107 });108 it('org units include attributes', async () => {109 const transform = [110 {111 insert: {112 name: '=orgUnitCodeToName($organisationUnit)',113 },114 transform: 'updateColumns',115 },116 ];117 const analytics = [118 { dataElement: 'BCD1', organisationUnit: 'TO_Facility1', period: '20210101', value: 1 },119 ];120 const data = { results: analytics };121 const context = await buildContext(transform, reqContext, data);122 const expectedContext = {123 orgUnits: [124 { id: 'ouId5', code: 'TO_Facility1', name: 'Tonga Facility 1', attributes: { x: 1 } },125 ],126 };127 expect(context).toStrictEqual(expectedContext);128 });129 it('ignores unknown entities', async () => {130 const transform = [131 {132 insert: {133 name: '=orgUnitCodeToName($organisationUnit)',134 },135 transform: 'updateColumns',136 },137 ];138 const analytics = [139 { dataElement: 'BCD1', organisationUnit: 'Unknown_entity', period: '20210101', value: 1 },140 ];141 const data = { results: analytics };142 const context = await buildContext(transform, reqContext, data);143 const expectedContext = {144 orgUnits: [],145 };146 expect(context).toStrictEqual(expectedContext);147 });148 });149 describe('dataElementCodeToName', () => {150 it('includes dataElementCodeToName from fetched data', async () => {151 const transform = [152 {153 insert: {154 name: '=dataElementCodeToName($dataElement)',155 },156 transform: 'updateColumns',157 },158 ];159 const analytics = [160 { dataElement: 'BCD1', organisationUnit: 'TO', period: '20210101', value: 1 },161 { dataElement: 'BCD2', organisationUnit: 'FJ', period: '20210101', value: 2 },162 ];163 const data = {164 results: analytics,165 metadata: {166 dataElementCodeToName: { BCD1: 'Facility Status', BCD2: 'Reason for closure' },167 },168 };169 const context = await buildContext(transform, reqContext, data);170 const expectedContext = {171 dataElementCodeToName: { BCD1: 'Facility Status', BCD2: 'Reason for closure' },172 };173 expect(context).toStrictEqual(expectedContext);174 });175 it('builds an empty object when using fetch events', async () => {176 const transform = [177 {178 insert: {179 name: '=dataElementCodeToName($dataElement)',180 },181 transform: 'updateColumns',182 },183 ];184 const events = [185 { event: 'evId1', eventDate: '2021-01-01T12:00:00', orgUnit: 'TO', orgUnitName: 'Tonga' },186 { event: 'evId2', eventDate: '2021-01-01T12:00:00', orgUnit: 'FJ', orgUnitName: 'Fiji' },187 ];188 const data = { results: events };189 const context = await buildContext(transform, reqContext, data);190 const expectedContext = { dataElementCodeToName: {} };191 expect(context).toStrictEqual(expectedContext);192 });193 });194 describe('facilityCountByOrgUnit', () => {195 it('builds facilityCountByOrgUnit using fetched analytics', async () => {196 const transform = ['insertNumberOfFacilitiesColumn'];197 const analytics = [198 { dataElement: 'BCD1', organisationUnit: 'TO', period: '20210101', value: 1 },199 { dataElement: 'BCD1', organisationUnit: 'FJ', period: '20210101', value: 2 },200 { dataElement: 'BCD1', organisationUnit: 'AU', period: '20210101', value: 2 },201 ];202 const data = { results: analytics };203 const context = await buildContext(transform, reqContext, data);204 const expectedContext = {205 facilityCountByOrgUnit: {206 TO: 2,207 FJ: 1,208 AU: 0,209 },210 };211 expect(context).toStrictEqual(expectedContext);212 });213 it('builds facilityCountByOrgUnit using fetched events', async () => {214 const transform = ['insertNumberOfFacilitiesColumn'];215 const events = [216 { event: 'evId1', eventDate: '2021-01-01T12:00:00', orgUnit: 'TO', orgUnitName: 'Tonga' },217 { event: 'evId2', eventDate: '2021-01-01T12:00:00', orgUnit: 'FJ', orgUnitName: 'Fiji' },218 ];219 const data = { results: events };220 const context = await buildContext(transform, reqContext, data);221 const expectedContext = {222 facilityCountByOrgUnit: {223 TO: 2,224 FJ: 1,225 },226 };227 expect(context).toStrictEqual(expectedContext);228 });229 it('ignores unknown entities', async () => {230 const transform = ['insertNumberOfFacilitiesColumn'];231 const analytics = [232 { dataElement: 'BCD1', organisationUnit: 'Unknown_entity', period: '20210101', value: 1 },233 ];234 const data = { results: analytics };235 const context = await buildContext(transform, reqContext, data);236 const expectedContext = {237 facilityCountByOrgUnit: {},238 };239 expect(context).toStrictEqual(expectedContext);240 });241 });...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1import { expectedContext } from 'fast-check';2const fc = expectedContext();3fc.assert(4 fc.property(fc.integer(), fc.integer(), (a, b) => {5 return a + b === b + a;6 }),7);8import { expectedContext } from 'fast-check';9const fc = expectedContext();10fc.assert(11 fc.property(fc.integer(), fc.integer(), (a, b) => {12 return a + b === b + a;13 }),14);

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require("fast-check");2fc.assert(fc.property(fc.integer(), fc.integer(), (a, b) => {3 return a + b === b + a;4}));5const fc = require("fast-check");6fc.assert(fc.property(fc.integer(), fc.integer(), (a, b) => {7 return a + b === b + a;8}));9const fc = require("fast-check");10fc.assert(fc.property(fc.integer(), fc.integer(), (a, b) => {11 return a + b === b + a;12}));13at Context.<anonymous> (test.js:11:12)14at processImmediate (internal/timers.js:439:21)15at process.topLevelDomainCallback (domain.js:130:23)

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const { expectedContext } = require('fast-check-monorepo');3const myArbitrary = fc.array(fc.integer(), 1, 10);4fc.assert(5 fc.property(myArbitrary, (arr) => {6 return arr.length > 0;7 })8);9fc.assert(10 fc.property(myArbitrary, (arr) => {11 return arr.length > 0;12 }),13 { verbose: true }14);15fc.assert(16 fc.property(myArbitrary, (arr) => {17 return arr.length > 0;18 }),19 { verbose: true, seed: 42 }20);21fc.assert(22 fc.property(myArbitrary, (arr) => {23 return arr.length > 0;24 }),25 { verbose: true, seed: 42, path: 'test.js' }26);27fc.assert(28 fc.property(myArbitrary, (arr) => {29 return arr.length > 0;30 }),31 { verbose: true, seed: 42, path: 'test.js', endOnFailure: true }32);33fc.assert(34 fc.property(myArbitrary, (arr) => {35 return arr.length > 0;36 }),37 { verbose: true, seed: 42, path: 'test.js', endOnFailure: true, expectedContext: expectedContext }38);39fc.assert(40 fc.property(myArbitrary, (arr) => {41 return arr.length > 0;42 }),43 { verbose: true, seed: 42, path: 'test.js', endOnFailure: true, expectedContext: expectedContext, skip: 1 }44);45fc.assert(46 fc.property(myArbitrary, (arr) => {47 return arr.length > 0;48 }),49 { verbose: true, seed: 42, path: 'test.js', endOnFailure: true, expectedContext: expectedContext, skip: 1, numRuns: 1 }50);51fc.assert(52 fc.property(myArbitrary, (arr) => {53 return arr.length > 0;54 }),55 { verbose: true, seed: 42, path: 'test.js', endOnFailure: true, expectedContext: expectedContext, skip: 1, numRuns: 1, timeout: 1 }56);57fc.assert(58 fc.property(myArbitrary, (arr) => {

Full Screen

Using AI Code Generation

copy

Full Screen

1const { expectedContext } = require('fast-check');2const fc = require('fast-check');3const { expect } = require('chai');4describe('test', () => {5 it('should test', () => {6 fc.assert(7 fc.property(fc.integer(), fc.integer(), (a, b) => {8 const context = expectedContext();9 expect(a + b).to.equal(b + a, context);10 })11 );12 });13});14const { expectedContext } = require('fast-check');15const fc = require('fast-check');16const { expect } = require('chai');17describe('test', () => {18 it('should test', () => {19 fc.assert(20 fc.property(fc.integer(), fc.integer(), (a, b) => {21 const context = expectedContext();22 expect(a + b).to.equal(b + a, context);23 })24 );25 });26});

Full Screen

Using AI Code Generation

copy

Full Screen

1import fc from 'fast-check';2import { expectedContext } from 'fast-check-monorepo';3const myContext = expectedContext();4fc.assert(fc.property(fc.integer(), fc.integer(), (a, b) => {5 const expected = a + b;6 const actual = myContext.add(a, b);7 return expected === actual;8}), { verbose: true, seed: 42 });9export function add(a, b) {10 return a + b;11}12✓ Property #1 failed after 1 tests (1 shrinks) •13 Error: Property failed after 1 tests (1 shrinks)

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const { expectedContext } = require('fast-check-monorepo');3const { it, expect } = expectedContext();4it('should work', () => {5 expect(fc.integer()).toBeGreaterThanOrEqual(0);6});7module.exports = {8};9const { it, expect } = require('fast-check-monorepo').expectedContext();10import { it, expect } from 'fast-check-monorepo';11it('should work', () => {12 expect(1 + 1).toBe(2);13});14it(title: string, callback: (done: () => void) => void | Promise<void>): void;15test(title: string, callback: (done: () => void) => void | Promise<void>): void;16expect<T>(actual: T): jest.Matchers<T>;

Full Screen

Using AI Code Generation

copy

Full Screen

1const { property } = require('fast-check');2const expectedContext = require('fast-check-monorepo');3test('test', () => {4 property(5 expectedContext(6 fc.integer(),7 fc.integer(),8 (a, b) => {9 expect(a).toBe(b);10 return true;11 },12 { verbose: true }13 );14});15const { property } = require('fast-check');16const expectedContext = require('fast-check');17test('test', () => {18 property(19 expectedContext(20 fc.integer(),21 fc.integer(),22 (a, b) => {23 expect(a).toBe(b);24 return true;25 },26 { verbose: true }27 );28});29{30 "scripts": {31 },32 "devDependencies": {33 }34}

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run fast-check-monorepo automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful