Best JavaScript code snippet using puppeteer
frames-test.js
Source:frames-test.js
1import {2 frameCreate,3 frameSetInitialResource,4 frameGoToResource,5 frameGoBack,6 frameGoBackToType,7 frameGoForward,8 frameEnableExtension,9 frameConfigureExtension,10 frameDisableExtension,11 frameNextCanvas,12 framePreviousCanvas,13 frameFocus,14 reducer,15 DEFAULT_STATE,16 frameDelete,17} from '../../src/spaces/frames';18describe('spaces/services', () => {19 describe('actions', () => {20 test('frameCreate', () => {21 expect(frameCreate()).toMatchSnapshot();22 expect(23 frameCreate({24 resourceId: 'https://iiif.com/collection-1.json',25 resourceType: 'collection',26 })27 ).toMatchSnapshot();28 expect(29 frameCreate(30 {31 resourceId: 'https://iiif.com/collection-1.json',32 resourceType: 'collection',33 },34 'my-custom-frame'35 )36 ).toMatchSnapshot();37 expect(38 frameCreate(39 {40 resourceId: 'https://iiif.com/collection-1.json',41 resourceType: 'collection',42 someConfigProp: { someBool: true },43 },44 'my-custom-frame'45 )46 ).toMatchSnapshot();47 });48 test('frameDelete', () => {49 expect(() => frameDelete()).toThrowErrorMatchingSnapshot();50 expect(frameDelete('frame-1')).toMatchSnapshot();51 });52 test('frameSetInitialResource', () => {53 expect(54 frameSetInitialResource({55 resourceId: 'https://iiif.com/collection-2.json',56 resourceType: 'collection',57 })58 ).toMatchSnapshot();59 expect(() => frameSetInitialResource()).toThrowErrorMatchingSnapshot();60 expect(61 frameSetInitialResource(62 {63 resourceId: 'https://iiif.com/collection-2.json',64 resourceType: 'collection',65 },66 'my-custom-frame'67 )68 ).toMatchSnapshot();69 });70 test('frameGoToResource', () => {71 expect(72 frameGoToResource({73 resourceId: 'https://iiif.com/collection-2.json',74 resourceType: 'collection',75 })76 ).toMatchSnapshot();77 expect(() => frameGoToResource()).toThrowErrorMatchingSnapshot();78 expect(79 frameGoToResource(80 {81 resourceId: 'https://iiif.com/collection-2.json',82 resourceType: 'collection',83 },84 'my-custom-frame'85 )86 ).toMatchSnapshot();87 expect(88 frameGoToResource(89 {90 resourceId: 'https://iiif.com/collection-2.json',91 resourceType: 'collection',92 smartJump: false,93 },94 'my-custom-frame'95 )96 ).toMatchSnapshot();97 });98 test('frameGoBack', () => {99 expect(frameGoBack()).toMatchSnapshot();100 expect(frameGoBack('my-custom-frame')).toMatchSnapshot();101 });102 test('frameGoBackToType', () => {103 expect(() => frameGoBackToType()).toThrowErrorMatchingSnapshot();104 expect(frameGoBackToType('collection')).toMatchSnapshot();105 expect(106 frameGoBackToType('manifest', 'my-custom-frame')107 ).toMatchSnapshot();108 });109 test('frameGoForward', () => {110 expect(frameGoForward()).toMatchSnapshot();111 expect(frameGoForward('my-custom-frame')).toMatchSnapshot();112 });113 test('frameEnableExtension', () => {114 expect(() => frameEnableExtension()).toThrowErrorMatchingSnapshot();115 expect(frameEnableExtension('extension-id-1')).toMatchSnapshot();116 expect(117 frameEnableExtension('extension-id-1', { someConfig: 'someValue' })118 ).toMatchSnapshot();119 expect(120 frameEnableExtension('extension-id-1', {}, 'my-custom-frame')121 ).toMatchSnapshot();122 });123 test('frameConfigureExtension', () => {124 expect(() => frameConfigureExtension()).toThrowErrorMatchingSnapshot();125 expect(frameConfigureExtension('extension-id-1')).toMatchSnapshot();126 expect(127 frameConfigureExtension('extension-id-1', { someConfig: 'someValue' })128 ).toMatchSnapshot();129 expect(130 frameConfigureExtension(131 'extension-id-1',132 { someConfig: 'someValue' },133 'my-custom-frame',134 true135 )136 ).toMatchSnapshot();137 expect(138 frameConfigureExtension('extension-id-1', {}, 'my-custom-frame')139 ).toMatchSnapshot();140 });141 test('frameDisableExtension', () => {142 expect(() => frameDisableExtension()).toThrowErrorMatchingSnapshot();143 expect(frameDisableExtension('my-extension')).toMatchSnapshot();144 expect(145 frameDisableExtension('my-extension', 'my-custom-frame')146 ).toMatchSnapshot();147 });148 test('frameNextCanvas', () => {149 expect(frameNextCanvas()).toMatchSnapshot();150 expect(frameNextCanvas('my-custom-frame')).toMatchSnapshot();151 });152 test('framePreviousCanvas', () => {153 expect(framePreviousCanvas()).toMatchSnapshot();154 expect(framePreviousCanvas('my-custom-frame')).toMatchSnapshot();155 });156 test('frameFocus', () => {157 expect(frameFocus()).toMatchSnapshot();158 expect(frameFocus('my-custom-frame')).toMatchSnapshot();159 });160 });161 describe('reducers', () => {162 const runActions = actions => actions.reduce(reducer, DEFAULT_STATE);163 test('default state', () => {164 expect(runActions([])).toMatchSnapshot();165 });166 describe('frameCreate', () => {167 test('simple create', () =>168 expect(runActions([frameCreate()])).toMatchSnapshot());169 test('create multiple', () =>170 expect(171 runActions([172 frameCreate({173 resourceId: 'https://iiif.com/collection-1.json',174 resourceType: 'collection',175 }),176 frameCreate(177 {178 resourceId: 'https://iiif.com/manifest-1.json',179 resourceType: 'manifest',180 },181 'manifest-frame'182 ),183 ])184 ).toMatchSnapshot());185 });186 test('frame delete', () => {187 expect(188 runActions([189 frameCreate(190 {191 resourceId: 'https://iiif.com/collection-1.json',192 resourceType: 'collection',193 },194 'frame-1'195 ),196 frameCreate(197 {198 resourceId: 'https://iiif.com/manifest-1.json',199 resourceType: 'manifest',200 },201 'frame-2'202 ),203 frameCreate(204 {205 resourceId: 'https://iiif.com/manifest-1.json',206 resourceType: 'manifest',207 },208 'frame-3'209 ),210 frameDelete('frame-1'),211 frameDelete('frame-3'),212 ])213 ).toMatchSnapshot();214 });215 describe('frameSetInitialResource', () => {216 test('set single resource', () =>217 expect(218 runActions([219 frameCreate(),220 frameSetInitialResource({221 resourceId: 'https://iiif.com/manifest-1.json',222 resourceType: 'manifest',223 }),224 ])225 ).toMatchSnapshot());226 test('set custom resource', () =>227 expect(228 runActions([229 frameCreate({230 resourceId: 'https://iiif.com/collection-1.json',231 resourceType: 'collection',232 }),233 frameCreate({}, 'my-custom-frame'),234 frameSetInitialResource(235 {236 resourceId: 'https://iiif.com/manifest-1.json',237 resourceType: 'manifest',238 },239 'my-custom-frame'240 ),241 ])242 ).toMatchSnapshot());243 });244 describe('frameGoToResource', () => {245 test('go to simple', () =>246 expect(247 runActions([248 frameCreate(),249 frameGoToResource({250 resourceId: 'https://iiif.com/collection-1.json',251 resourceType: 'collection',252 }),253 ])254 ).toMatchSnapshot());255 test('go to deep', () =>256 expect(257 runActions([258 frameCreate(),259 frameGoToResource({260 resourceId: 'https://iiif.com/collection-1.json',261 resourceType: 'collection',262 }),263 frameGoToResource({264 resourceId: 'https://iiif.com/collection-2.json',265 resourceType: 'collection',266 }),267 frameGoToResource({268 resourceId: 'https://iiif.com/manifest-1.json',269 resourceType: 'manifest',270 }),271 ])272 ).toMatchSnapshot());273 test('go to back-track (smart=true)', () =>274 expect(275 runActions([276 frameCreate(),277 frameGoToResource({278 resourceId: 'https://iiif.com/collection-1.json',279 resourceType: 'collection',280 }),281 frameGoToResource({282 resourceId: 'https://iiif.com/collection-2.json',283 resourceType: 'collection',284 }),285 frameGoToResource({286 resourceId: 'https://iiif.com/manifest-1.json',287 resourceType: 'manifest',288 }),289 frameGoToResource({290 resourceId: 'https://iiif.com/collection-2.json',291 resourceType: 'collection',292 }),293 ])294 ).toMatchSnapshot());295 test('go to back-track (smart=false)', () =>296 expect(297 runActions([298 frameCreate(),299 frameGoToResource({300 resourceId: 'https://iiif.com/collection-1.json',301 resourceType: 'collection',302 }),303 frameGoToResource({304 resourceId: 'https://iiif.com/collection-2.json',305 resourceType: 'collection',306 }),307 frameGoToResource({308 resourceId: 'https://iiif.com/manifest-1.json',309 resourceType: 'manifest',310 }),311 frameGoToResource({312 resourceId: 'https://iiif.com/collection-2.json',313 resourceType: 'collection',314 smartJump: false,315 }),316 ])317 ).toMatchSnapshot());318 });319 describe('frameGoBack/Forward', () => {320 test('go back simple', () =>321 expect(322 runActions([323 frameCreate(),324 frameGoToResource({325 resourceId: 'https://iiif.com/collection-1.json',326 resourceType: 'collection',327 }),328 frameGoToResource({329 resourceId: 'https://iiif.com/collection-2.json',330 resourceType: 'collection',331 }),332 frameGoToResource({333 resourceId: 'https://iiif.com/collection-3.json',334 resourceType: 'collection',335 }),336 frameGoBack(),337 ])338 ).toMatchSnapshot());339 test('go back too far', () =>340 expect(341 runActions([342 frameCreate(),343 frameGoToResource({344 resourceId: 'https://iiif.com/collection-1.json',345 resourceType: 'collection',346 }),347 frameGoToResource({348 resourceId: 'https://iiif.com/collection-2.json',349 resourceType: 'collection',350 }),351 frameGoToResource({352 resourceId: 'https://iiif.com/collection-3.json',353 resourceType: 'collection',354 }),355 frameGoBack(),356 frameGoBack(),357 frameGoBack(),358 frameGoBack(),359 frameGoBack(),360 ])361 ).toMatchSnapshot());362 test('go back and forward', () =>363 expect(364 runActions([365 frameCreate(),366 frameGoToResource({367 resourceId: 'https://iiif.com/collection-1.json',368 resourceType: 'collection',369 }),370 frameGoToResource({371 resourceId: 'https://iiif.com/collection-2.json',372 resourceType: 'collection',373 }),374 frameGoToResource({375 resourceId: 'https://iiif.com/collection-3.json',376 resourceType: 'collection',377 }),378 frameGoBack(),379 frameGoBack(),380 frameGoForward(),381 ])382 ).toMatchSnapshot());383 test('go forward too far', () =>384 expect(385 runActions([386 frameCreate(),387 frameGoToResource({388 resourceId: 'https://iiif.com/collection-1.json',389 resourceType: 'collection',390 }),391 frameGoToResource({392 resourceId: 'https://iiif.com/collection-2.json',393 resourceType: 'collection',394 }),395 frameGoToResource({396 resourceId: 'https://iiif.com/collection-3.json',397 resourceType: 'collection',398 }),399 frameGoToResource({400 resourceId: 'https://iiif.com/collection-4.json',401 resourceType: 'collection',402 }),403 frameGoBack(),404 frameGoBack(),405 frameGoForward(),406 frameGoForward(),407 frameGoForward(),408 frameGoForward(),409 ])410 ).toMatchSnapshot());411 });412 describe('extensions', () => {413 test('enable extension', () =>414 expect(415 runActions([416 frameCreate({417 resourceId: 'https://iiif.com/collection-1.json',418 resourceType: 'collection',419 }),420 frameEnableExtension('my-awesome-extension'),421 ])422 ).toMatchSnapshot());423 test('enable with string config', () =>424 expect(425 runActions([426 frameCreate({427 resourceId: 'https://iiif.com/collection-1.json',428 resourceType: 'collection',429 }),430 frameEnableExtension(431 'my-awesome-extension',432 'instance-id-of-extension'433 ),434 ])435 ).toMatchSnapshot());436 test('enable with js config', () =>437 expect(438 runActions([439 frameCreate({440 resourceId: 'https://iiif.com/collection-1.json',441 resourceType: 'collection',442 }),443 frameEnableExtension('my-awesome-extension', {444 inlineConfig: 'something value',445 }),446 ])447 ).toMatchSnapshot());448 test('disable extension', () =>449 expect(450 runActions([451 frameCreate({452 resourceId: 'https://iiif.com/collection-1.json',453 resourceType: 'collection',454 }),455 frameEnableExtension('my-awesome-extension', {456 inlineConfig: 'something value',457 }),458 frameDisableExtension('my-awesome-extension'),459 ])460 ).toMatchSnapshot());461 test('disable non-existent extension', () =>462 expect(463 runActions([464 frameCreate({465 resourceId: 'https://iiif.com/collection-1.json',466 resourceType: 'collection',467 }),468 frameEnableExtension('my-awesome-extension', {469 inlineConfig: 'something value',470 }),471 frameDisableExtension('my-awesome-extension-NOT_REAL'),472 ])473 ).toMatchSnapshot());474 test('disable enable twice - act like config', () =>475 expect(476 runActions([477 frameCreate({478 resourceId: 'https://iiif.com/collection-1.json',479 resourceType: 'collection',480 }),481 frameEnableExtension('my-awesome-extension', {482 inlineConfig: 'something value',483 }),484 frameEnableExtension('my-awesome-extension', {485 someOtherValue: 'something value',486 }),487 ])488 ).toMatchSnapshot());489 test('configure', () =>490 expect(491 runActions([492 frameCreate({493 resourceId: 'https://iiif.com/collection-1.json',494 resourceType: 'collection',495 }),496 frameEnableExtension('my-awesome-extension'),497 frameConfigureExtension('my-awesome-extension', {498 inlineConfig: 'something value',499 loaded: true,500 }),501 ])502 ).toMatchSnapshot());503 });504 test('focus frame', () => {505 expect(506 runActions([507 frameCreate(508 {509 resourceId: 'https://iiif.com/collection-1.json',510 resourceType: 'collection',511 },512 'frame-1'513 ),514 frameCreate(515 {516 resourceId: 'https://iiif.com/collection-2.json',517 resourceType: 'collection',518 },519 'frame-2'520 ),521 frameCreate(522 {523 resourceId: 'https://iiif.com/collection-3.json',524 resourceType: 'collection',525 },526 'frame-3'527 ),528 frameFocus('frame-2'),529 ])530 ).toMatchSnapshot();531 });532 test('frameConfigureExtension', () => {533 expect(534 runActions([535 frameCreate(),536 frameConfigureExtension('extension-1', { key1: 0 }, 'default-frame'),537 frameConfigureExtension(538 'extension-1',539 { key1: 1, key2: 2 },540 'default-frame'541 ),542 frameConfigureExtension(543 'extension-1',544 { key3: 3, key2: 4 },545 'default-frame',546 true547 ),548 ])549 ).toMatchSnapshot();550 });551 });...
history-test.js
Source:history-test.js
1import {2 frameCreate,3 frameGoBack,4 frameGoToResource,5} from '../../../src/spaces/frames';6import { makeStateFromActions } from '../../../test-utils';7import frame from '../../../src/api/frame';8describe('iiif/api/frame history', () => {9 const defaultFrame = frame(s => s.frames.list['default-frame']);10 describe('getTopLevel', () => {11 test('empty frame', () => {12 const state = makeStateFromActions(frameCreate());13 expect(defaultFrame.getTopLevel(state)).toEqual(null);14 });15 test('from initial resource', () => {16 const state = makeStateFromActions(17 frameCreate({18 resourceId: 'https://iiif.com/collection-1.json',19 resourceType: 'collection',20 })21 );22 expect(defaultFrame.getTopLevel(state)).toEqual({23 id: 'https://iiif.com/collection-1.json',24 schema: 'collection',25 });26 });27 test('from navigated resource', () => {28 const state = makeStateFromActions(29 frameCreate({30 resourceId: 'https://iiif.com/collection-1.json',31 resourceType: 'collection',32 }),33 frameGoToResource({34 resourceId: 'https://iiif.com/collection-2.json',35 resourceType: 'collection',36 }),37 frameGoToResource({38 resourceId: 'https://iiif.com/manifest-1.json',39 resourceType: 'manifest',40 })41 );42 expect(defaultFrame.getTopLevel(state)).toEqual({43 id: 'https://iiif.com/collection-1.json',44 schema: 'collection',45 });46 });47 });48 describe('getAllTypes', () => {49 test('single type', () => {50 const state = makeStateFromActions(51 frameCreate({52 resourceId: 'https://iiif.com/collection-1.json',53 resourceType: 'collection',54 })55 );56 expect(defaultFrame.getAllTypes(state)).toEqual(['collection']);57 });58 test('multiple types', () => {59 const state = makeStateFromActions(60 frameCreate({61 resourceId: 'https://iiif.com/collection-1.json',62 resourceType: 'collection',63 }),64 frameGoToResource({65 resourceId: 'https://iiif.com/collection-2.json',66 resourceType: 'collection',67 }),68 frameGoToResource({69 resourceId: 'https://iiif.com/manifest-1.json',70 resourceType: 'manifest',71 })72 );73 expect(defaultFrame.getAllTypes(state)).toEqual([74 'collection',75 'manifest',76 ]);77 });78 });79 describe('canGoBack', () => {80 test('empty frame', () => {81 const state = makeStateFromActions(frameCreate());82 expect(defaultFrame.canGoBack(state)).toEqual(false);83 });84 test('single resource', () => {85 const state = makeStateFromActions(86 frameCreate({87 resourceId: 'https://iiif.com/collection-1.json',88 resourceType: 'collection',89 })90 );91 expect(defaultFrame.canGoBack(state)).toEqual(false);92 });93 test('multiple resources', () => {94 const state = makeStateFromActions(95 frameCreate({96 resourceId: 'https://iiif.com/collection-1.json',97 resourceType: 'collection',98 }),99 frameGoToResource({100 resourceId: 'https://iiif.com/collection-2.json',101 resourceType: 'collection',102 })103 );104 expect(defaultFrame.canGoBack(state)).toEqual(true);105 });106 test('multiple resources - nav back', () => {107 const state = makeStateFromActions(108 frameCreate({109 resourceId: 'https://iiif.com/collection-1.json',110 resourceType: 'collection',111 }),112 frameGoToResource({113 resourceId: 'https://iiif.com/collection-2.json',114 resourceType: 'collection',115 }),116 frameGoBack()117 );118 expect(defaultFrame.canGoBack(state)).toEqual(false);119 });120 });121 describe('canGoForward', () => {122 test('empty frame', () => {123 const state = makeStateFromActions(frameCreate());124 expect(defaultFrame.canGoForward(state)).toEqual(false);125 });126 test('single resource', () => {127 const state = makeStateFromActions(128 frameCreate({129 resourceId: 'https://iiif.com/collection-1.json',130 resourceType: 'collection',131 })132 );133 expect(defaultFrame.canGoForward(state)).toEqual(false);134 });135 test('multiple resources', () => {136 const state = makeStateFromActions(137 frameCreate({138 resourceId: 'https://iiif.com/collection-1.json',139 resourceType: 'collection',140 }),141 frameGoToResource({142 resourceId: 'https://iiif.com/collection-2.json',143 resourceType: 'collection',144 })145 );146 expect(defaultFrame.canGoForward(state)).toEqual(false);147 });148 test('multiple resources - nav back', () => {149 const state = makeStateFromActions(150 frameCreate({151 resourceId: 'https://iiif.com/collection-1.json',152 resourceType: 'collection',153 }),154 frameGoToResource({155 resourceId: 'https://iiif.com/collection-2.json',156 resourceType: 'collection',157 }),158 frameGoBack(),159 frameGoBack()160 );161 expect(defaultFrame.canGoForward(state)).toEqual(true);162 });163 test('multiple resources - nav back but not all the way', () => {164 const state = makeStateFromActions(165 frameCreate({166 resourceId: 'https://iiif.com/collection-1.json',167 resourceType: 'collection',168 }),169 frameGoToResource({170 resourceId: 'https://iiif.com/collection-2.json',171 resourceType: 'collection',172 }),173 frameGoToResource({174 resourceId: 'https://iiif.com/collection-3.json',175 resourceType: 'collection',176 }),177 frameGoToResource({178 resourceId: 'https://iiif.com/collection-4.json',179 resourceType: 'collection',180 }),181 frameGoBack(),182 frameGoBack()183 );184 expect(defaultFrame.canGoForward(state)).toEqual(true);185 });186 });187 describe('getHistory', () => {188 test('nav forward', () => {189 const state = makeStateFromActions(190 frameCreate({191 resourceId: 'https://iiif.com/collection-1.json',192 resourceType: 'collection',193 }),194 frameGoToResource({195 resourceId: 'https://iiif.com/collection-2.json',196 resourceType: 'collection',197 })198 );199 expect(defaultFrame.getHistory(state)).toEqual([200 { id: 'https://iiif.com/collection-1.json', schema: 'collection' },201 ]);202 });203 test('nav backward', () => {204 const state = makeStateFromActions(205 frameCreate({206 resourceId: 'https://iiif.com/collection-1.json',207 resourceType: 'collection',208 }),209 frameGoToResource({210 resourceId: 'https://iiif.com/collection-2.json',211 resourceType: 'collection',212 }),213 frameGoToResource({214 resourceId: 'https://iiif.com/collection-3.json',215 resourceType: 'collection',216 }),217 frameGoBack()218 );219 expect(defaultFrame.getHistory(state)).toEqual([220 { id: 'https://iiif.com/collection-1.json', schema: 'collection' },221 ]);222 });223 });224 describe('getFullHistory', () => {225 test('nav forward', () => {226 const state = makeStateFromActions(227 frameCreate({228 resourceId: 'https://iiif.com/collection-1.json',229 resourceType: 'collection',230 }),231 frameGoToResource({232 resourceId: 'https://iiif.com/collection-2.json',233 resourceType: 'collection',234 })235 );236 expect(defaultFrame.getFullHistory(state)).toEqual([237 { id: 'https://iiif.com/collection-1.json', schema: 'collection' },238 { id: 'https://iiif.com/collection-2.json', schema: 'collection' },239 ]);240 });241 test('nav backward', () => {242 const state = makeStateFromActions(243 frameCreate({244 resourceId: 'https://iiif.com/collection-1.json',245 resourceType: 'collection',246 }),247 frameGoToResource({248 resourceId: 'https://iiif.com/collection-2.json',249 resourceType: 'collection',250 }),251 frameGoToResource({252 resourceId: 'https://iiif.com/collection-3.json',253 resourceType: 'collection',254 }),255 frameGoBack()256 );257 expect(defaultFrame.getFullHistory(state)).toEqual([258 { id: 'https://iiif.com/collection-1.json', schema: 'collection' },259 { id: 'https://iiif.com/collection-2.json', schema: 'collection' },260 { id: 'https://iiif.com/collection-3.json', schema: 'collection' },261 ]);262 });263 });264 describe('canGoBackToType', () => {265 test('empty frame', () => {266 const state = makeStateFromActions(frameCreate());267 expect(defaultFrame.canGoBackToType('collection')(state)).toEqual(false);268 });269 test('single resource - true', () => {270 const state = makeStateFromActions(271 frameCreate({272 resourceId: 'https://iiif.com/collection-1.json',273 resourceType: 'collection',274 }),275 frameGoToResource({276 resourceId: 'https://iiif.com/collection-2.json',277 resourceType: 'collection',278 })279 );280 expect(defaultFrame.canGoBackToType('collection')(state)).toEqual(true);281 });282 test('single resource - false', () => {283 const state = makeStateFromActions(284 frameCreate({285 resourceId: 'https://iiif.com/collection-1.json',286 resourceType: 'collection',287 })288 );289 expect(defaultFrame.canGoBackToType('manifest')(state)).toEqual(false);290 });291 test('multiple resource', () => {292 const state = makeStateFromActions(293 frameCreate({294 resourceId: 'https://iiif.com/collection-1.json',295 resourceType: 'collection',296 }),297 frameGoToResource({298 resourceId: 'https://iiif.com/manifest-1.json',299 resourceType: 'manifest',300 }),301 frameGoToResource({302 resourceId: 'https://iiif.com/canvas-1.json',303 resourceType: 'canvas',304 })305 );306 expect(defaultFrame.canGoBackToType('collection')(state)).toEqual(true);307 expect(defaultFrame.canGoBackToType('manifest')(state)).toEqual(true);308 expect(defaultFrame.canGoBackToType('canvas')(state)).toEqual(false);309 expect(defaultFrame.canGoBackToType('sequence')(state)).toEqual(false);310 });311 });...
server.js
Source:server.js
1var utils = require('utilities')2 , restify = require('restify')3 , model = require('../../../../lib')4 , helpers = require('.././helpers')5 , MemoryAdapter = require('../../../../lib/adapters/memory').Adapter6 , adapter7 , server8 , shared = require('../shared');9function init()10{11 var relations = helpers.fixtures.slice()12 , models = [];13 adapter = new MemoryAdapter();14 relations.forEach(function (r) {15 models.push({16 ctorName: r.ctorName17 , ctor: r.ctor18 });19 });20 model.clearDefinitions(models);21 model.registerDefinitions(models);22 model.adapters = {};23 relations.forEach(function (r) {24 model[r.ctorName].adapter = adapter;25 });26 // create mock server27 server = restify.createServer();28 server.use(restify.acceptParser(server.acceptable));29 server.use(restify.queryParser());30 server.use(restify.bodyParser());31 server.use(logRequest);32 server.pre(function (req, res, next) {33 // strip file format suffixes34 req.url = req.url.replace(/\.json/,'');35 return next();36 });37 server.get('/beforeEach', function(req, resp, next) {38 shared.beforeEach(function() {39 res.send({});40 next()41 });42 });43 server.get('/afterEach', function(req, resp, next) {44 shared.afterEach(function() {45 res.send({});46 next();47 });48 });49 // GET all50 server.get('/:resourceType', function (req, res, next) {51 getAll(req.params.resourceType, req.params.query, getValidOptions(req.params), function(err, resp) {52 res.send(resp);53 return next();54 });55 });56 // GET first by id57 server.get('/:resourceType/:id', function (req, res, next) {58 getFirst(req.params.resourceType, req.params.id, function(err, resp) {59 res.send(resp);60 return next();61 });62 });63 // update64 server.put('/:resourceType/:id', function (req, res, next) {65 updateFirst(req.params.resourceType, req.params.id, req.params, function(err, resp) {66 res.send(resp);67 return next();68 });69 });70 // remove71 server.del('/:resourceType', function(req, res, next) {72 removeAll(req.params.resourceType, req.params.query, getValidOptions(req.params), function(err, resp) {73 res.send(resp);74 return next();75 });76 });77 server.del('/:resourceType/:id', function(req, res, next) {78 removeFirst(req.params.resourceType, req.params.id, req.params, function(err, resp) {79 res.send(resp);80 return next();81 });82 });83 // Browser compliant post: Update/Remove first by id84 server.post('/:resourceType/:id', function (req, res, next) {85 res.header("Access-Control-Allow-Origin", "*");86 res.header("Access-Control-Allow-Headers", "X-Requested-With");87 if (req.params._method === 'PUT') {88 updateFirst(req.params.resourceType, req.params.id, req.params, function(err, resp) {89 res.send(resp);90 return next();91 });92 }93 else if(req.params._method === 'DELETE') {94 removeFirst(req.params.resourceType, req.params.id, req.params, function(err, resp) {95 res.send(resp);96 return next();97 });98 }99 else {100 throw new Error('method must be PUT or DELETE.');101 return next();102 }103 });104 // insert new105 server.post('/:resourceType', function (req, res, next) {106 res.header("Access-Control-Allow-Origin", "*");107 res.header("Access-Control-Allow-Headers", "X-Requested-With");108 create(req.params.resourceType, req.params, function(err, resp) {109 res.send(resp);110 return next();111 });112 });113 server.listen(3000, function() {114 console.log('listening at localhost:3000');115 });116}117function logRequest(req, resp, next) {118 console.log((new Date()) + ' - ' + req.method + ': ' + req.url);119 if (req.route) {120 console.log('\troute: ' + req.route.path);121 }122 if (next) {123 return next();124 }125 else {126 return;127 }128}129function getAll(resourceType, query, opts, cb) {130 // normalize resourceType131 resourceType = utils.string.getInflection(resourceType, 'constructor', 'singular');132 if (model[resourceType]) {133 var res = {};134 model[resourceType].all(query || {}, opts || {}, function (err, all) {135 if (err) {136 throw err;137 return;138 }139 if (opts && opts.limit === 1) {140 all = [all];141 }142 var pluralName = utils.string.getInflection(resourceType, 'property', 'plural');143 res[pluralName] = all || [];144 res.query = query;145 res.count = (all) ? all.length : 0;146 cb(null, res);147 });148 }149 else {150 var err = new Error('Resource "' + resourceType + '" does not exist.', 404)151 throw err;152 }153}154function getFirst(resourceType, query, cb)155{156 // normalize resourceType157 resourceType = utils.string.getInflection(resourceType, 'constructor', 'singular');158 if (model[resourceType]) {159 var res = {};160 model[resourceType].first(query, function(err, first) {161 if (err) {162 throw err;163 return;164 }165 var all = [first];166 var pluralName = utils.string.getInflection(resourceType, 'property', 'plural');167 res[pluralName] = all || [];168 res.count = (all) ? all.length : 0;169 cb(null, res);170 });171 }172 else {173 var err = new Error('Resource "' + resourceType + '" does not exist.', 404);174 cb(err, {175 error: getErrorObject(err)176 });177 }178}179function updateFirst(resourceType, query, data, cb)180{181 // normalize resourceType182 resourceType = utils.string.getInflection(resourceType, 'constructor', 'singular');183 var propType = utils.string.getInflection(resourceType, 'property', 'singular');184 if (model[resourceType]) {185 var res = {};186 model[resourceType].first(query, function(err, first) {187 if (err) {188 throw err;189 return;190 }191 if (first) {192 first.updateProperties(data[propType]);193 if (first.isValid()) {194 first.save(onSaved);195 }196 else {197 first.save(onSaved);198 }199 }200 });201 }202 else {203 var err = new Error('Resource "' + resourceType + '" does not exist.', 404);204 throw err;205 }206 function onSaved(err, first) {207 if (err) {208 throw err;209 return;210 }211 else {212 var resp = {};213 var pluralName = utils.string.getInflection(resourceType, 'property', 'plural');214 resp[pluralName] = [first];215 cb(null, resp);216 }217 }218}219function removeFirst(resourceType, id, params, cb)220{221 // normalize resourceType222 resourceType = utils.string.getInflection(resourceType, 'constructor', 'singular');223 if (model[resourceType]) {224 var res = {};225 model[resourceType].remove(id, function(err, data) {226 if (err) {227 throw err;228 return;229 }230 cb(null, {231 success: true,232 data: data233 });234 });235 }236 else {237 var err = new Error('Resource "' + resourceType + '" does not exist.', 404);238 throw err;239 }240}241function removeAll(resourceType, query, opts, cb)242{243 // if no query is given do nothing244 if (!query) {245 cb(null, { success: true });246 return;247 }248 // normalize resourceType249 resourceType = utils.string.getInflection(resourceType, 'constructor', 'singular');250 if (model[resourceType]) {251 var res = {};252 model[resourceType].remove(query, opts, function(err, data) {253 if (err) {254 throw err;255 return;256 }257 cb(null, {258 success: true,259 data: data260 });261 });262 }263 else {264 var err = new Error('Resource "' + resourceType + '" does not exist.', 404);265 throw err;266 }267}268function create(resourceType, params, cb)269{270 // normalize resourceType271 resourceType = utils.string.getInflection(resourceType, 'constructor', 'singular');272 var propType = utils.string.getInflection(resourceType, 'property', 'singular');273 if (model[resourceType]) {274 var resource = model[resourceType].create(params[propType]);275 if (resource.isValid()) {276 resource.save(function(err, data) {277 if (err) {278 throw err;279 return;280 }281 else {282 var resp = {};283 var pluralName = utils.string.getInflection(resourceType, 'property', 'plural');284 resp[pluralName] = [resource.toJSON()];285 cb(null, resp);286 }287 });288 }289 else {290 var resp = {};291 var pluralName = utils.string.getInflection(resourceType, 'property', 'plural');292 resp[pluralName] = [resource.toJSON()];293 cb(null, resp);294 }295 }296 else {297 var err = new Error('Resource "' + resourceType + '" does not exist.', 404);298 throw err;299 }300}301function getErrorObject(error) {302 console.error(error);303 return {304 message: error.message,305 stack: error.stack,306 code: error.code307 }308}309function getValidOptions(params)310{311 var opts = {};312 if (params.limit) opts.limit = parseInt(params.limit);313 if (params.offset) opts.offset = parseInt(params.offset);314 if (params.page && params.per) {315 opts.limit = parseInt(params.per);316 opts.offset = (parseInt(params.page) - 1) * opts.limit;317 }318 if (params.sort) opts.sort = params.sort;319 if (params.nocase) opts.nocase = (params.nocase === true || params.nocase === 'true' || params.nocase === '1');320 return opts;321}...
current-test.js
Source:current-test.js
1import {2 frameCreate,3 frameFocus,4 frameGoBack,5 frameGoToResource,6} from '../../../src/spaces/frames';7import frame, { frames, focusedFrame } from '../../../src/api/frame';8import { makeStateFromActions } from '../../../test-utils';9describe('iiif/api/frame metadata', () => {10 const defaultFrame = frame(s => s.frames.list['default-frame']);11 test('getCurrentPath', () => {12 const state = makeStateFromActions(13 frameCreate({14 resourceId: 'https://iiif.com/collection-1.json',15 resourceType: 'collection',16 })17 );18 expect(defaultFrame.getCurrentPath(state)).toMatchSnapshot();19 });20 describe('getCurrentPathIndex', () => {21 test('initial index', () => {22 const state = makeStateFromActions(23 frameCreate({24 resourceId: 'https://iiif.com/collection-1.json',25 resourceType: 'collection',26 })27 );28 expect(defaultFrame.getCurrentPathIndex(state)).toEqual(0);29 });30 test('after navigation', () => {31 const state = makeStateFromActions(32 frameCreate({33 resourceId: 'https://iiif.com/collection-1.json',34 resourceType: 'collection',35 }),36 frameGoToResource({37 resourceId: 'https://iiif.com/collection-2.json',38 resourceType: 'collection',39 }),40 frameGoToResource({41 resourceId: 'https://iiif.com/manifest-1.json',42 resourceType: 'manifest',43 })44 );45 expect(defaultFrame.getCurrentPathIndex(state)).toEqual(2);46 });47 test('after back', () => {48 const state = makeStateFromActions(49 frameCreate({50 resourceId: 'https://iiif.com/collection-1.json',51 resourceType: 'collection',52 }),53 frameGoToResource({54 resourceId: 'https://iiif.com/collection-2.json',55 resourceType: 'collection',56 }),57 frameGoToResource({58 resourceId: 'https://iiif.com/manifest-1.json',59 resourceType: 'manifest',60 }),61 frameGoBack()62 );63 expect(defaultFrame.getCurrentPathIndex(state)).toEqual(1);64 });65 });66 describe('getCurrentResource{Id,Type}', () => {67 test('empty', () => {68 const state = makeStateFromActions(frameCreate());69 expect(defaultFrame.getCurrentResourceId(state)).toEqual(null);70 expect(defaultFrame.getCurrentResourceType(state)).toEqual(null);71 });72 test('initial current', () => {73 const state = makeStateFromActions(74 frameCreate({75 resourceId: 'https://iiif.com/collection-1.json',76 resourceType: 'collection',77 })78 );79 expect(defaultFrame.getCurrentResource(state)).toEqual({80 id: 'https://iiif.com/collection-1.json',81 schema: 'collection',82 });83 expect(defaultFrame.getCurrentResourceType(state)).toEqual('collection');84 expect(defaultFrame.getCurrentResourceId(state)).toEqual(85 'https://iiif.com/collection-1.json'86 );87 });88 test('after navigation', () => {89 const state = makeStateFromActions(90 frameCreate({91 resourceId: 'https://iiif.com/collection-1.json',92 resourceType: 'collection',93 }),94 frameGoToResource({95 resourceId: 'https://iiif.com/collection-2.json',96 resourceType: 'collection',97 }),98 frameGoToResource({99 resourceId: 'https://iiif.com/manifest-1.json',100 resourceType: 'manifest',101 })102 );103 expect(defaultFrame.getCurrentResource(state)).toEqual({104 id: 'https://iiif.com/manifest-1.json',105 schema: 'manifest',106 });107 expect(defaultFrame.getCurrentResourceType(state)).toEqual('manifest');108 expect(defaultFrame.getCurrentResourceId(state)).toEqual(109 'https://iiif.com/manifest-1.json'110 );111 });112 });113 describe('getResourceByType', () => {114 test('get first collection', () => {115 const state = makeStateFromActions(116 frameCreate({117 resourceId: 'https://iiif.com/collection-1.json',118 resourceType: 'collection',119 }),120 frameGoToResource({121 resourceId: 'https://iiif.com/collection-2.json',122 resourceType: 'collection',123 })124 );125 expect(126 defaultFrame.getCurrentResourceByType('collection')(state)127 ).toEqual({128 id: 'https://iiif.com/collection-2.json',129 schema: 'collection',130 });131 });132 test('get first collection when on manifest', () => {133 const state = makeStateFromActions(134 frameCreate({135 resourceId: 'https://iiif.com/collection-1.json',136 resourceType: 'collection',137 }),138 frameGoToResource({139 resourceId: 'https://iiif.com/collection-2.json',140 resourceType: 'collection',141 }),142 frameGoToResource({143 resourceId: 'https://iiif.com/manifest-1.json',144 resourceType: 'manifest',145 })146 );147 expect(148 defaultFrame.getCurrentResourceByType('collection')(state)149 ).toEqual({150 id: 'https://iiif.com/collection-2.json',151 schema: 'collection',152 });153 expect(defaultFrame.getCurrentResourceByType('manifest')(state)).toEqual({154 id: 'https://iiif.com/manifest-1.json',155 schema: 'manifest',156 });157 expect(defaultFrame.getCurrentResourceByType('NOT_EXIST')(state)).toEqual(158 null159 );160 });161 describe('focused frame', () => {162 test('focused frame without navigation', () => {163 const state = makeStateFromActions(164 frameCreate(165 {166 resourceId: 'https://iiif.com/collection-1.json',167 resourceType: 'collection',168 },169 'frame-1'170 ),171 frameCreate(172 {173 resourceId: 'https://iiif.com/collection-2.json',174 resourceType: 'collection',175 },176 'frame-2'177 ),178 frameCreate(179 {180 resourceId: 'https://iiif.com/collection-3.json',181 resourceType: 'collection',182 },183 'frame-3'184 )185 );186 const selector = focusedFrame(api => ({187 id: api.getId,188 }));189 expect(selector(state)).toEqual({ id: 'frame-3' });190 });191 test('focused frame with navigation', () => {192 const state = makeStateFromActions(193 frameCreate(194 {195 resourceId: 'https://iiif.com/collection-1.json',196 resourceType: 'collection',197 },198 'frame-1'199 ),200 frameCreate(201 {202 resourceId: 'https://iiif.com/collection-2.json',203 resourceType: 'collection',204 },205 'frame-2'206 ),207 frameCreate(208 {209 resourceId: 'https://iiif.com/collection-3.json',210 resourceType: 'collection',211 },212 'frame-3'213 ),214 frameFocus('frame-1')215 );216 const selector = focusedFrame(api => ({217 id: api.getId,218 }));219 expect(selector(state)).toEqual({ id: 'frame-1' });220 });221 });222 });223 test('all frames', () => {224 const state = makeStateFromActions(225 frameCreate(226 {227 resourceId: 'https://iiif.com/collection-1.json',228 resourceType: 'collection',229 },230 'frame-1'231 ),232 frameCreate(233 {234 resourceId: 'https://iiif.com/collection-2.json',235 resourceType: 'collection',236 },237 'frame-2'238 ),239 frameCreate(240 {241 resourceId: 'https://iiif.com/collection-3.json',242 resourceType: 'collection',243 },244 'frame-3'245 )246 );247 const selector = frames(248 () => ['frame-1', 'frame-2'],249 api => ({250 id: api.getId,251 resourceId: api.getCurrentResourceId,252 })253 );254 expect(selector(state)).toEqual([255 { id: 'frame-1', resourceId: 'https://iiif.com/collection-1.json' },256 { id: 'frame-2', resourceId: 'https://iiif.com/collection-2.json' },257 ]);258 });...
resourceIndex.js
Source:resourceIndex.js
1/*2 * Base controller inherited from by the resource index controllers3 */4configmanagerControllers.controller('ResourceIndexCtrl', ['$scope', 'ConfigerTabs', 'Configer', 'Errors',5 function($scope, ConfigerTabs, Configer, Errors){6 var expanded = false;7 var loading = false;8 9 $scope.views = {};10 $scope.status = {};11 $scope.data = {};12 $scope.data.currentConfigerId = Configer.getCurrent() == undefined ? undefined : Configer.getCurrent().id;13 $scope.data.resources = [];14 $scope.toolbar = {};15 $scope.toolbar.buttons = [16 { 17 id: "new", 18 text: "New",19 show: true, 20 icon: "glyphicon glyphicon-plus",21 click: function() {22 newResource();23 }24 },25 { 26 id: "toggleDetails", 27 text: "Expand All", 28 show: true, 29 icon: "glyphicon glyphicon-chevron-down",30 click: function() {31 toggleAllDetails();32 }33 }34 ];35 36 function wrapResource(resource, resourceType, showDetails) {37 return {38 data: resource,39 resourceType: resourceType,40 tagId: resource.id > 0 ? resourceType + '_' + resource.id : undefined,41 showDetails: showDetails,42 detailsToggleTip: 'Show Details',43 detailsToggleIcon: 'icon-chevron-down',44 showPartial: $scope.views.resource45 };46 }47 48 function newResource() {49 // broadcast a message down through the child scopes we want to create a new resource50 $scope.$broadcast('resource.new');51 };52 53 function toggleAllDetails() {54 expanded = !expanded;55 for (var k = 0; k < $scope.data.resources.length; k++) {56 $scope.data.resources[k].showDetails = expanded;57 58 $scope.data.resources[k].detailsToggleTip = expanded ? 'Hide Details' : 'Show Details';59 $scope.data.resources[k].detailsToggleIcon = expanded ? 'glyphicon glyphicon-chevron-up' : 'glyphicon glyphicon-chevron-down';60 }61 $scope.toolbar.buttons[1].text = expanded ? "Collapse All" : "Expand All";62 $scope.toolbar.buttons[1].icon = expanded ? "glyphicon glyphicon-chevron-up" : "glyphicon glyphicon-chevron-down";63 };64 65 $scope.pushResources = function(resources, resourceType) {66 for (var k = 0; k < resources.length; k++)67 $scope.pushResource(resources[k], resourceType);68 };69 70 $scope.pushResource = function(resource, resourceType) {71 $scope.data.resources.push(wrapResource(resource, resourceType, false));72 };73 74 $scope.insertResource = function(index, resource, resourceType) {75 $scope.data.resources.splice(index, 0, wrapResource(resource, resourceType, true));76 };77 78 $scope.removeResource = function(resourceId) {79 for (var k = 0; k < $scope.data.resources.length; k++) {80 var r = $scope.data.resources[k];81 if (r.data.id == resourceId) {82 $scope.data.resources.splice(k, 1);83 break;84 }85 }86 };87 88 $scope.beginLoadResources = function() {89 loading = true;90 $scope.data.resources.length = 0;91 };92 93 $scope.endLoadResources = function(isError) {94 loading = false;95 var resourceType = $scope.status.resourceType;96 if (isError) {97 $scope.status.message = "There was an error loading your " + resourceType + ". Please try again.";98 } else {99 $scope.status.message = $scope.data.resources.length == 0 ? 100 "You currently have no " + resourceType + ". Click the 'New' button to add a " + resourceType + "." :101 undefined;102 }103 };104 105 $scope.$watchCollection('data.resources', function(newValue,oldValue){106 var resourceType = $scope.status.resourceType;107 if (loading){108 $scope.status.message = "Loading your " + resourceType + ". One moment please.";109 } else {110 $scope.status.message = $scope.data.resources.length == 0 ? 111 "You currently have no " + resourceType + ". Click the 'New' button to add a " + resourceType + "." :112 undefined;113 }114 });115 116 $scope.$on('configer.selected', function() {117 $scope.data.currentConfigerId = Configer.getCurrent().id;118 });119 120 $scope.$on('resource.remove', function(e, id){121 $scope.removeResource(id);122 });...
actions.js
Source:actions.js
1import { actionTypes as resourceActions } from 'redux-resource';2const getAllDefaults = {3 requestKey: 'getAll',4}5export const getAllPending = (resourceType, actionProps) => ({6 ...getAllDefaults,7 ...actionProps,8 resourceType,9 type: resourceActions.READ_RESOURCES_PENDING,10});11export const getAllSucceeded = (resourceType, response, transformResp = (resp) => { return resp.data.results }, actionProps) => ({12 ...getAllDefaults,13 ...actionProps,14 resourceType,15 type: resourceActions.READ_RESOURCES_SUCCEEDED,16 resources: transformResp(response),17});18export const getAllFailed = (resourceType, err, actionProps) => ({19 ...getAllDefaults,20 ...actionProps,21 resourceType,22 type: resourceActions.READ_RESOURCES_FAILED,23});24const findDefaults = {25 requestKey: 'find',26}27export const findPending = (resourceType, actionProps) => ({28 ...findDefaults,29 ...actionProps,30 resourceType,31 type: resourceActions.READ_RESOURCES_PENDING,32});33export const findSucceeded = (resourceType, response, transformResp = (resp) => { return [resp.data] }, actionProps) => ({34 ...findDefaults,35 ...actionProps,36 resourceType,37 type: resourceActions.READ_RESOURCES_SUCCEEDED,38 resources: transformResp(response),39});40export const findFailed = (resourceType, err, actionProps) => ({41 ...findDefaults,42 ...actionProps,43 resourceType,44 type: resourceActions.READ_RESOURCES_FAILED,45});46const updateDefaults = {47 requestKey: 'update',48}49export const updatePending = (resourceType, actionProps) => ({50 ...updateDefaults,51 ...actionProps,52 resourceType,53 type: resourceActions.UPDATE_RESOURCES_PENDING,54});55export const updateSucceeded = (resourceType, response, transformResp = (resp) => { return [resp.data] }, actionProps) => ({56 ...updateDefaults,57 ...actionProps,58 resourceType,59 type: resourceActions.UPDATE_RESOURCES_SUCCEEDED,60 resources: transformResp(response),61});62export const updateFailed = (resourceType, err, actionProps) => ({63 ...updateDefaults,64 ...actionProps,65 resourceType,66 type: resourceActions.UPDATE_RESOURCES_FAILED,67});68export const getHistoryPending = (resourceType, [_, page]) => ({69 requestKey: `getHistory${page}`,70 list: `userHistory${page}`,71 resourceType,72 type: resourceActions.READ_RESOURCES_PENDING,73});74export const getHistorySucceeded = (resourceType, response, [_, page], transformResp = (resp) => resp.data.results) => {75 const { next, previous } = response.data;76 return {77 requestKey: `getHistory${page}`,78 list: `userHistory${page}`,79 resourceType,80 type: resourceActions.READ_RESOURCES_SUCCEEDED,81 resources: transformResp(response),82 requestProperties: {83 next,84 previous85 }86 };87};88export const getHistoryFailed = (resourceType, [_, page]) => ({89 requestKey: `getHistory${page}`,90 list: `userHistory${page}`,91 resourceType,92 type: resourceActions.READ_RESOURCES_FAILED,...
app.js
Source:app.js
1import { Invoice } from './classes/invoice.js';2import { ListTemplate } from './classes/list-template.js';3import { Payment } from './classes/payment.js';4const docTypeEnum = {5 invoice: 'invoice',6 payment: 'payment'7};8const form = document.querySelector('.new-item-form');9// inputs10const type = document.querySelector('#type');11const toFrom = document.querySelector('#toFrom');12const details = document.querySelector('#details');13const amount = document.querySelector('#amount');14// list template instance15const ul = document.querySelector('ul');16const list = new ListTemplate(ul);17form.addEventListener('submit', (e) => {18 e.preventDefault();19 let values;20 values = [toFrom.value, details.value, amount.valueAsNumber];21 let doc;22 if (type.value === docTypeEnum.invoice) {23 doc = new Invoice(...values);24 }25 else {26 doc = new Payment(...values);27 }28 list.render(doc, type.value, 'end');29});30//ENUM31var ResourceType;32(function (ResourceType) {33 ResourceType[ResourceType["BOOK"] = 0] = "BOOK";34 ResourceType[ResourceType["AUTHOR"] = 1] = "AUTHOR";35 ResourceType[ResourceType["FILM"] = 2] = "FILM";36 ResourceType[ResourceType["DIRECTOR"] = 3] = "DIRECTOR";37 ResourceType[ResourceType["PERSON"] = 4] = "PERSON";38})(ResourceType || (ResourceType = {}));39// Generic40const addUID = (obj) => {41 let uid = Math.floor(Math.random() * 100);42 return Object.assign(Object.assign({}, obj), { uid });43};44let docOne = addUID({ name: 'Yoshi', age: 40 });45console.log(docOne.age);46const docTwo = {47 uid: 1,48 resourceNameID: ResourceType.PERSON,49 data: 'Rohan'50};51// tuples52let arr = ['ryu', 25, true];53arr[0] = false;54arr[1] = 'yoshi';55arr = [30, false, 'yoshi'];56let tup = ['ryu', 25, true];...
Enums.js
Source:Enums.js
1"use strict";2// Enums3var ResourceType;4(function (ResourceType) {5 ResourceType[ResourceType["MOVIE"] = 0] = "MOVIE";6 ResourceType[ResourceType["PERSON"] = 1] = "PERSON";7 ResourceType[ResourceType["AUTHOR"] = 2] = "AUTHOR";8 ResourceType[ResourceType["DVD"] = 3] = "DVD";9 ResourceType[ResourceType["CD"] = 4] = "CD";10})(ResourceType || (ResourceType = {}));11const docFive = {12 uid: 6,13 resourceType: ResourceType.DVD,14 data: { title: 'best love' }15};16const docSix = {17 uid: 11,18 resourceType: ResourceType.MOVIE,19 data: { name: 'jemol' }...
Using AI Code Generation
1const puppeteer = require('puppeteer');2(async () => {3 const browser = await puppeteer.launch();4 const page = await browser.newPage();5 const resourceType = await page.evaluate(() => document.resourceType);6 console.log(`Resource type of the page is ${resourceType}`);7 await browser.close();8})();9const puppeteer = require('puppeteer');10(async () => {11 const browser = await puppeteer.launch();12 const page = await browser.newPage();13 const title = await page.evaluate(() => document.title);14 console.log(`Title of the page is ${title}`);15 await browser.close();16})();17const puppeteer = require('puppeteer');18(async () => {19 const browser = await puppeteer.launch();20 const page = await browser.newPage();21 const url = await page.evaluate(() => document.URL);22 console.log(`URL of the page is ${url}`);23 await browser.close();24})();25const puppeteer = require('puppeteer');26(async () => {27 const browser = await puppeteer.launch();28 const page = await browser.newPage();29 const viewport = await page.evaluate(() => document.viewport);30 console.log(`Viewport of the page is ${viewport}`);31 await browser.close();32})();33const puppeteer = require('puppeteer');34(async () => {35 const browser = await puppeteer.launch();36 const page = await browser.newPage();37 await page.evaluate(() => document.write('Hello World!'));38 await browser.close();39})();40const puppeteer = require('puppeteer');41(async () => {42 const browser = await puppeteer.launch();43 const page = await browser.newPage();44 await page.evaluate(() => document.writeln('Hello World!'));45 await browser.close();46})();
Using AI Code Generation
1const puppeteer = require('puppeteer');2(async () => {3 const browser = await puppeteer.launch({headless: false});4 const page = await browser.newPage();5 await page.type('input[title="Search"]', 'Puppeteer');6 await page.keyboard.press('Enter');7 await page.waitForNavigation();8 await page.waitForSelector('div#search');9 await page.waitForSelector('div#search a');10 const links = await page.evaluate(() => {11 const anchors = Array.from(document.querySelectorAll('div#search a'));12 return anchors.map(anchor => anchor.textContent);13 });14 console.log(links.join('\n'));15 await browser.close();16})();17const puppeteer = require('puppeteer');18(async () => {19 const browser = await puppeteer.launch({headless: false});20 const page = await browser.newPage();21 await page.type('input[title="Search"]', 'Puppeteer');22 await page.keyboard.press('Enter');23 await page.waitForNavigation();24 await page.waitForSelector('div#search');25 await page.waitForSelector('div#search a');26 const links = await page.evaluate(() => {27 const anchors = Array.from(document.querySelectorAll('div#search a'));28 return anchors.map(anchor => anchor.textContent);29 });30 console.log(links.join('\n'));31 await browser.close();32})();33const puppeteer = require('puppeteer');34(async () => {35 const browser = await puppeteer.launch({headless: false});36 const page = await browser.newPage();37 await page.type('input[title="Search"]', 'Puppeteer');38 await page.keyboard.press('Enter');39 await page.waitForNavigation();40 await page.waitForSelector('div#search');41 await page.waitForSelector('div#search a');42 const links = await page.evaluate(() => {43 const anchors = Array.from(document.querySelectorAll('div#search a'));44 return anchors.map(anchor => anchor.textContent);45 });46 console.log(links.join('\n'));47 await browser.close();48})();
Using AI Code Generation
1const puppeteer = require('puppeteer');2(async () => {3 const browser = await puppeteer.launch({headless: false});4 const page = await browser.newPage();5 ];6 const requests = new Map();7 page.on('request', request => {8 requests.set(request, request.resourceType());9 });10 page.on('requestfinished', request => {11 requests.set(request, `${request.resourceType()} (finished)`);12 });13 page.on('requestfailed', request => {14 requests.set(request, `${request.resourceType()} (failed)`);15 });16 for (const [request, resourceType] of requests) {17 console.log(`${request.url()} is a ${resourceType}`);18 }19 await browser.close();20})();21const puppeteer = require('puppeteer');22(async () => {23 const browser = await puppeteer.launch({headless: false});24 const page = await browser.newPage();25 await page.setExtraHTTPHeaders({26 });27 await page.screenshot({path: 'example.png'});28 await browser.close();29})();30const puppeteer = require('puppeteer');31(async () => {32 const browser = await puppeteer.launch({headless: false});33 const page = await browser.newPage();34 await page.setRequestInterception(true);35 page.on('request', interceptedRequest => {36 if (interceptedRequest.url().endsWith('.png') ||37 interceptedRequest.url().endsWith('.jpg'))38 interceptedRequest.abort();39 interceptedRequest.continue();40 });41 await browser.close();42})();43const puppeteer = require('puppeteer
Using AI Code Generation
1const puppeteer = require('puppeteer');2(async () => {3 const browser = await puppeteer.launch();4 const page = await browser.newPage();5 await page.screenshot({path: 'google.png'});6 await browser.close();7})();8const puppeteer = require('puppeteer');9(async () => {10 const browser = await puppeteer.launch();11 const page = await browser.newPage();12 await page.screenshot({path: 'google.png'});13 await browser.close();14})();15const puppeteer = require('puppeteer');16(async () => {17 const browser = await puppeteer.launch();18 const page = await browser.newPage();19 await page.screenshot({path: 'google.png'});20 await browser.close();21})();22const puppeteer = require('puppeteer');23(async () => {24 const browser = await puppeteer.launch();25 const page = await browser.newPage();26 await page.screenshot({path: 'google.png'});27 await browser.close();28})();29const puppeteer = require('puppeteer');30(async () => {31 const browser = await puppeteer.launch();32 const page = await browser.newPage();33 await page.screenshot({path: 'google.png'});34 await browser.close();35})();36const puppeteer = require('puppeteer');37(async () => {38 const browser = await puppeteer.launch();39 const page = await browser.newPage();40 await page.screenshot({path: 'google.png'});41 await browser.close();42})();43const puppeteer = require('puppeteer');44(async () => {45 const browser = await puppeteer.launch();46 const page = await browser.newPage();47 await page.screenshot({path: 'google.png'});48 await browser.close();49})();
Using AI Code Generation
1const puppeteer = require('puppeteer');2(async () => {3 const browser = await puppeteer.launch();4 const page = await browser.newPage();5 const resourceType = await page.evaluate(() => document.resourceType);6 console.log(resourceType);7 await browser.close();8})();
Using AI Code Generation
1const puppeteer = require('puppeteer');2(async () => {3 const browser = await puppeteer.launch();4 const page = await browser.newPage();5 const element = await page.$('#hplogo');6 const resourceType = await page.evaluate(element => element.resourceType(), element);7 console.log(resourceType);8 await browser.close();9})();10const puppeteer = require('puppeteer');11(async () => {12 const browser = await puppeteer.launch();13 const page = await browser.newPage();14 await page.screenshot({path: 'google.png'});15 await browser.close();16})();17const puppeteer = require('puppeteer');18(async () => {19 const browser = await puppeteer.launch();20 const page = await browser.newPage();21 await page.screenshot({path: 'google.png', fullPage: true});22 await browser.close();23})();24const puppeteer = require('puppeteer');25(async () => {26 const browser = await puppeteer.launch();27 const page = await browser.newPage();28 await page.screenshot({path: 'google.png', fullPage: true, type: 'jpeg', quality: 0});29 await browser.close();30})();31const puppeteer = require('puppeteer');32(async () => {33 const browser = await puppeteer.launch();34 const page = await browser.newPage();35 await page.screenshot({path: 'google.pdf', fullPage: true, type: 'pdf'});36 await browser.close();37})();38const puppeteer = require('puppeteer');39(async () => {40 const browser = await puppeteer.launch();41 const page = await browser.newPage();42 await page.select('#searchLanguage', 'en');43 await browser.close();44})();
Using AI Code Generation
1const puppeteer = require('puppeteer');2(async () => {3 const browser = await puppeteer.launch();4 const page = await browser.newPage();5 await page.type('input[title="Search"]', 'puppeteer');6 await page.keyboard.press('Enter');7 await page.waitForNavigation();8 const resourceType = await page.resourceType();9 console.log(resourceType);10 await browser.close();11})();12const puppeteer = require('puppeteer');13(async () => {14 const browser = await puppeteer.launch();15 const page = await browser.newPage();16 await page.type('input[title="Search"]', 'puppeteer');17 await page.keyboard.press('Enter');18 await page.waitForNavigation();19 const cookies = await page.cookies();20 console.log(cookies);21 await browser.close();22})();23const puppeteer = require('puppeteer');24(async () => {25 const browser = await puppeteer.launch();26 const page = await browser.newPage();27 await page.type('input[title="Search"]', 'puppeteer');28 await page.keyboard.press('Enter');29 await page.waitForNavigation();30 await page.setCookie({31 });32 await page.setCookie({33 });34 await page.setCookie({35 });36 const cookies = await page.cookies();37 console.log(cookies
Using AI Code Generation
1(async () => {2 const browser = await puppeteer.launch();3 const page = await browser.newPage();4 const resourceTypes = await page.$$eval('img', imgs => imgs.map(img => img.src));5 console.log(resourceTypes);6 await browser.close();7})();
Using AI Code Generation
1const puppeteer = require('puppeteer');2const resourceTypes = new Set([3 ]);4(async () => {5 const browser = await puppeteer.launch();6 const page = await browser.newPage();7 await page.setRequestInterception(true);8 page.on('request', (request) => {9 if (!resourceTypes.has(request.resourceType())) {10 request.abort();11 } else {12 request.continue();13 }14 });15 await page.goto(url);16 await page.screenshot({path: 'example.png'});17 await browser.close();18})();19const puppeteer = require('puppeteer');20const resourceTypes = new Set([21 ]);22(async () => {23 const browser = await puppeteer.launch();24 const page = await browser.newPage();25 await page.setRequestInterception(true);26 page.on('request', (request) => {27 if (!resourceTypes.has(request.resourceType())) {28 request.abort();29 } else {30 request.continue();31 }32 });33 await page.goto(url);34 await page.screenshot({path: 'example.png'});35 await browser.close();36})();37const puppeteer = require('puppeteer');38const resourceTypes = new Set([39 ]);40(async () => {41 const browser = await puppeteer.launch();42 const page = await browser.newPage();43 await page.setRequestInterception(true);
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!