Best JavaScript code snippet using best
cache.test.js
Source:cache.test.js
1// @formatter:off2'use strict'3import Cache from './cache'4import CachePage from './cache-page'5import Queue from '../queue'6import Messages from '../messages'7describe('As a developer, I need a data object', function () {8 beforeAll(() => {9 console.log('BEGIN CACHE TEST ===========================================')10 })11 beforeEach(() => {12 })13 afterEach(() => {14 Queue.unregisterAll()15 })16 afterAll(() => {17 })18 it ('should be be able to create a object and control its maximum size', () => {19 Queue.unregisterAll()20 let maxPages = 521 let cache = new Cache(maxPages)22 expect(cache.maxPages).toBe(maxPages)23 })24 it ('should be be able to add a page, even when full, and get the pages back out', () => {25 let maxPages = 526 let cache = new Cache(maxPages)27 let cachePage1 = new CachePage('DATA', 0, 'PARENT', CachePage.NEVER_EXPIRES, null, false)28 let cachePage2 = new CachePage('DATA', 1, 'PARENT', CachePage.NEVER_EXPIRES, null, false)29 let cachePage3 = new CachePage('DATA', 2, 'PARENT', CachePage.NEVER_EXPIRES, null, false)30 let cachePage4 = new CachePage('DATA', 3, 'PARENT', CachePage.NEVER_EXPIRES, null, false)31 let cachePage5 = new CachePage('DATA', 4, 'PARENT', CachePage.NEVER_EXPIRES, null, false)32 let cachePage6 = new CachePage('DATA', 5, 'PARENT', CachePage.NEVER_EXPIRES, null, false)33 cache.addPage(cachePage1)34 expect(cache.getPageCount()).toBe(1)35 expect(cache.hasPage(cachePage1.pageNumber)).toBe(true)36 expect(cache.getPage(cachePage1.pageNumber)).toBe(cachePage1)37 expect(cache.isFull()).toBe(false)38 cache.addPage(cachePage2)39 expect(cache.getPageCount()).toBe(2)40 expect(cache.hasPage(cachePage1.pageNumber)).toBe(true)41 expect(cache.getPage(cachePage1.pageNumber)).toBe(cachePage1)42 expect(cache.hasPage(cachePage2.pageNumber)).toBe(true)43 expect(cache.getPage(cachePage2.pageNumber)).toBe(cachePage2)44 expect(cache.isFull()).toBe(false)45 cache.addPage(cachePage3)46 expect(cache.getPageCount()).toBe(3)47 expect(cache.hasPage(cachePage1.pageNumber)).toBe(true)48 expect(cache.getPage(cachePage1.pageNumber)).toBe(cachePage1)49 expect(cache.hasPage(cachePage2.pageNumber)).toBe(true)50 expect(cache.getPage(cachePage2.pageNumber)).toBe(cachePage2)51 expect(cache.hasPage(cachePage3.pageNumber)).toBe(true)52 expect(cache.getPage(cachePage3.pageNumber)).toBe(cachePage3)53 expect(cache.isFull()).toBe(false)54 cache.addPage(cachePage4)55 expect(cache.getPageCount()).toBe(4)56 expect(cache.hasPage(cachePage1.pageNumber)).toBe(true)57 expect(cache.getPage(cachePage1.pageNumber)).toBe(cachePage1)58 expect(cache.hasPage(cachePage2.pageNumber)).toBe(true)59 expect(cache.getPage(cachePage2.pageNumber)).toBe(cachePage2)60 expect(cache.hasPage(cachePage3.pageNumber)).toBe(true)61 expect(cache.getPage(cachePage3.pageNumber)).toBe(cachePage3)62 expect(cache.hasPage(cachePage4.pageNumber)).toBe(true)63 expect(cache.getPage(cachePage4.pageNumber)).toBe(cachePage4)64 expect(cache.isFull()).toBe(false)65 cache.addPage(cachePage5)66 expect(cache.getPageCount()).toBe(5)67 expect(cache.hasPage(cachePage1.pageNumber)).toBe(true)68 expect(cache.getPage(cachePage1.pageNumber)).toBe(cachePage1)69 expect(cache.hasPage(cachePage2.pageNumber)).toBe(true)70 expect(cache.getPage(cachePage2.pageNumber)).toBe(cachePage2)71 expect(cache.hasPage(cachePage3.pageNumber)).toBe(true)72 expect(cache.getPage(cachePage3.pageNumber)).toBe(cachePage3)73 expect(cache.hasPage(cachePage4.pageNumber)).toBe(true)74 expect(cache.getPage(cachePage4.pageNumber)).toBe(cachePage4)75 expect(cache.hasPage(cachePage5.pageNumber)).toBe(true)76 expect(cache.getPage(cachePage5.pageNumber)).toBe(cachePage5)77 expect(cache.isFull()).toBe(true) // <---78 cache.addPage(cachePage6)79 expect(cache.getPageCount()).toBe(5) // <---80 expect(cache.hasPage(cachePage1.pageNumber)).toBe(false) // <---81 expect(cache.getPage(cachePage1.pageNumber)).toBe(null) // <---82 expect(cache.hasPage(cachePage2.pageNumber)).toBe(true)83 expect(cache.getPage(cachePage2.pageNumber)).toBe(cachePage2)84 expect(cache.hasPage(cachePage3.pageNumber)).toBe(true)85 expect(cache.getPage(cachePage3.pageNumber)).toBe(cachePage3)86 expect(cache.hasPage(cachePage4.pageNumber)).toBe(true)87 expect(cache.getPage(cachePage4.pageNumber)).toBe(cachePage4)88 expect(cache.hasPage(cachePage5.pageNumber)).toBe(true)89 expect(cache.getPage(cachePage5.pageNumber)).toBe(cachePage5)90 expect(cache.hasPage(cachePage6.pageNumber)).toBe(true)91 expect(cache.getPage(cachePage6.pageNumber)).toBe(cachePage6)92 expect(cache.isFull()).toBe(true)93 })94 it ('should return null when requesting a page not in the object.', () => {95 let maxPages = 596 let cache = new Cache(maxPages)97 let cachePage1 = new CachePage('DATA', 0, 'PARENT', CachePage.NEVER_EXPIRES, null, false)98 let cachePage2 = new CachePage('DATA', 1, 'PARENT', CachePage.NEVER_EXPIRES, null, false)99 let cachePage3 = new CachePage('DATA', 2, 'PARENT', CachePage.NEVER_EXPIRES, null, false)100 let cachePage4 = new CachePage('DATA', 3, 'PARENT', CachePage.NEVER_EXPIRES, null, false)101 let cachePage5 = new CachePage('DATA', 4, 'PARENT', CachePage.NEVER_EXPIRES, null, false)102 cache.addPage(cachePage1)103 cache.addPage(cachePage2)104 cache.addPage(cachePage3)105 cache.addPage(cachePage4)106 cache.addPage(cachePage5)107 expect(cache.hasPage(5)).toBe(false)108 expect(cache.getPage(5)).toBe(null)109 })110 it ('should know when is has data.', () => {111 let maxPages = 5112 let cache = new Cache(maxPages)113 let cachePage1 = new CachePage('DATA', 0, 'PARENT', CachePage.NEVER_EXPIRES, null, false)114 let cachePage2 = new CachePage('DATA', 1, 'PARENT', CachePage.NEVER_EXPIRES, null, false)115 let cachePage3 = new CachePage('DATA', 2, 'PARENT', CachePage.NEVER_EXPIRES, null, false)116 let cachePage4 = new CachePage('DATA', 3, 'PARENT', CachePage.NEVER_EXPIRES, null, false)117 let cachePage5 = new CachePage('DATA', 4, 'PARENT', CachePage.NEVER_EXPIRES, null, false)118 expect(cache.hasData(0)).toBe(false)119 expect(cache.hasData(1)).toBe(false)120 expect(cache.hasData(2)).toBe(false)121 expect(cache.hasData(3)).toBe(false)122 expect(cache.hasData(4)).toBe(false)123 cache.addPage(cachePage1)124 cache.addPage(cachePage2)125 cache.addPage(cachePage3)126 cache.addPage(cachePage4)127 cache.addPage(cachePage5)128 expect(cache.hasData(0)).toBe(true)129 expect(cache.hasData(1)).toBe(true)130 expect(cache.hasData(2)).toBe(true)131 expect(cache.hasData(3)).toBe(true)132 expect(cache.hasData(4)).toBe(true)133 expect(cache.hasData(5)).toBe(false)134 })135 it ('should be able to remove a page.', () => {136 let maxPages = 5137 let cache = new Cache(maxPages)138 let cachePage1 = new CachePage('DATA', 0, 'PARENT', CachePage.NEVER_EXPIRES, null, false)139 let cachePage2 = new CachePage('DATA', 1, 'PARENT', CachePage.NEVER_EXPIRES, null, false)140 let cachePage3 = new CachePage('DATA', 2, 'PARENT', CachePage.NEVER_EXPIRES, null, false)141 let cachePage4 = new CachePage('DATA', 3, 'PARENT', CachePage.NEVER_EXPIRES, null, false)142 let cachePage5 = new CachePage('DATA', 4, 'PARENT', CachePage.NEVER_EXPIRES, null, false)143 cache.addPage(cachePage1)144 cache.addPage(cachePage2)145 cache.addPage(cachePage3)146 cache.addPage(cachePage4)147 cache.addPage(cachePage5)148 expect(cache.hasPage(0)).toBe(true)149 let removedPages = cache.removePage(0)150 expect(cache.hasPage(0)).toBe(false)151 expect(removedPages.length).toBe(1)152 expect(removedPages[0]).toBe(cachePage1)153 removedPages = cache.removePage(9999)154 expect(removedPages).toBe(null)155 })156 it ('should be able to get expired pages.', () => {157 let maxPages = 5158 let cache = new Cache(maxPages)159 let cachePage1 = new CachePage('DATA', 0, 'PARENT', CachePage.NEVER_EXPIRES, null, false)160 let cachePage2 = new CachePage('DATA', 1, 'PARENT', CachePage.NEVER_EXPIRES, null, false)161 let cachePage3 = new CachePage('DATA', 2, 'PARENT', CachePage.NEVER_EXPIRES, null, false)162 let cachePage4 = new CachePage('DATA', 3, 'PARENT', CachePage.NEVER_EXPIRES, null, false)163 let cachePage5 = new CachePage('DATA', 4, 'PARENT', CachePage.NEVER_EXPIRES, null, false)164 cache.addPage(cachePage1)165 cache.addPage(cachePage2)166 cache.addPage(cachePage3)167 cache.addPage(cachePage4)168 cache.addPage(cachePage5)169 cachePage1.expireNow()170 cachePage3.expireNow()171 cachePage5.expireNow()172 let expiredPages = cache.getExpiredPages()173 expect(expiredPages.length).toBe(3)174 expect(expiredPages[0]).toBe(cachePage1)175 expect(expiredPages[1]).toBe(cachePage3)176 expect(expiredPages[2]).toBe(cachePage5)177 expect(cache.hasPage(cachePage1.pageNumber)).toBe(true)178 expect(cache.hasPage(cachePage3.pageNumber)).toBe(true)179 expect(cache.hasPage(cachePage5.pageNumber)).toBe(true)180 })181 it ('should be able to remove the oldest page.', () => {182 let maxPages = 5183 let cache = new Cache(maxPages)184 let cachePage1 = new CachePage('DATA', 0, 'PARENT', CachePage.NEVER_EXPIRES, null, false)185 let cachePage2 = new CachePage('DATA', 1, 'PARENT', CachePage.NEVER_EXPIRES, null, false)186 let cachePage3 = new CachePage('DATA', 2, 'PARENT', CachePage.NEVER_EXPIRES, null, false)187 let cachePage4 = new CachePage('DATA', 3, 'PARENT', CachePage.NEVER_EXPIRES, null, false)188 let cachePage5 = new CachePage('DATA', 4, 'PARENT', CachePage.NEVER_EXPIRES, null, false)189 let now = new Date().getTime()190 cache.addPage(cachePage1)191 cache.addPage(cachePage2)192 cache.addPage(cachePage3)193 cache.addPage(cachePage4)194 cache.addPage(cachePage5)195 cachePage1.createdAt = now + 10196 cachePage2.createdAt = now + 20197 cachePage3.createdAt = now + 30198 cachePage4.createdAt = now + 40199 cachePage5.createdAt = now + 50200 let removedPage = cache.removeOldestPage(9999)201 expect(removedPage.length).toBe(1)202 expect(removedPage[0]).toBe(cachePage1)203 expect(cache.getPageCount()).toBe(4)204 expect(cache.hasPage(cachePage1.pageNumber)).toBe(false)205 removedPage = cache.removeOldestPage(1) // <-- keep pageNumber 1206 expect(removedPage.length).toBe(1)207 expect(removedPage[0]).toBe(cachePage3)208 expect(cache.getPageCount()).toBe(3)209 expect(cache.hasPage(cachePage3.pageNumber)).toBe(false)210 })211 it ('should be able to remove the furthest page.', () => {212 let maxPages = 5213 let cache = new Cache(maxPages)214 let cachePage1 = new CachePage('DATA', 0, 'PARENT', CachePage.NEVER_EXPIRES, null, false)215 let cachePage2 = new CachePage('DATA', 1, 'PARENT', CachePage.NEVER_EXPIRES, null, false)216 let cachePage3 = new CachePage('DATA', 2, 'PARENT', CachePage.NEVER_EXPIRES, null, false)217 let cachePage4 = new CachePage('DATA', 3, 'PARENT', CachePage.NEVER_EXPIRES, null, false)218 let cachePage5 = new CachePage('DATA', 4, 'PARENT', CachePage.NEVER_EXPIRES, null, false)219 cache.addPage(cachePage1)220 cache.addPage(cachePage2)221 cache.addPage(cachePage3)222 cache.addPage(cachePage4)223 cache.addPage(cachePage5)224 let removedPage = cache.removeFurthestPage(4) // <-- furthest from pageNumber 4225 expect(removedPage.length).toBe(1)226 expect(removedPage[0]).toBe(cachePage1)227 expect(cache.getPageCount()).toBe(4)228 expect(cache.hasPage(cachePage1.pageNumber)).toBe(false)229 removedPage = cache.removeFurthestPage(1) // <-- furthest from pageNumber 1230 expect(removedPage.length).toBe(1)231 expect(removedPage[0]).toBe(cachePage5)232 expect(cache.getPageCount()).toBe(3)233 expect(cache.hasPage(cachePage5.pageNumber)).toBe(false)234 removedPage = cache.removeFurthestPage(3) // <-- furthest from pageNumber 3235 expect(removedPage.length).toBe(1)236 expect((removedPage[0] === cachePage2) || (removedPage[0] === cachePage4)).toBe(true)237 expect(cache.getPageCount()).toBe(2)238 expect(cache.hasPage(cachePage2.pageNumber) && cache.hasPage(cachePage4.pageNumber)).toBe(false)239 expect(cache.hasPage(cachePage2.pageNumber) || cache.hasPage(cachePage4.pageNumber)).toBe(true)240 })241 it ('should be able to clear the object', () => {242 let maxPages = 5243 let cache = new Cache(maxPages)244 let cachePage1 = new CachePage('DATA', 0, 'PARENT', CachePage.NEVER_EXPIRES, null, false)245 let cachePage2 = new CachePage('DATA', 1, 'PARENT', CachePage.NEVER_EXPIRES, null, false)246 let cachePage3 = new CachePage('DATA', 2, 'PARENT', CachePage.NEVER_EXPIRES, null, false)247 let cachePage4 = new CachePage('DATA', 3, 'PARENT', CachePage.NEVER_EXPIRES, null, false)248 let cachePage5 = new CachePage('DATA', 4, 'PARENT', CachePage.NEVER_EXPIRES, null, false)249 cache.addPage(cachePage1)250 cache.addPage(cachePage2)251 cache.addPage(cachePage3)252 cache.addPage(cachePage4)253 cache.addPage(cachePage5)254 cache.clear()255 expect(cache.hasPage(cachePage1.pageNumber)).toBe(false)256 expect(cache.hasPage(cachePage2.pageNumber)).toBe(false)257 expect(cache.hasPage(cachePage3.pageNumber)).toBe(false)258 expect(cache.hasPage(cachePage4.pageNumber)).toBe(false)259 expect(cache.hasPage(cachePage5.pageNumber)).toBe(false)260 })261 it ('should be able to splice a page', () => {262 let maxPages = 5263 let cache = new Cache(maxPages)264 let cachePage1 = new CachePage('DATA', 0, 'PARENT', CachePage.NEVER_EXPIRES, null, false)265 let cachePage2 = new CachePage('DATA', 0, 'PARENT', CachePage.NEVER_EXPIRES, null, false)266 let cachePage3 = new CachePage('DATA', 1, 'PARENT', CachePage.NEVER_EXPIRES, null, false)267 cache.addPage(cachePage1)268 expect(cache.pages.length).toBe(1)269 expect(cache.pages[0]).toBe(cachePage1)270 let splicedPage = cache.splicePage(cachePage2) // <--- Same pageNumber as cachePage1271 expect(splicedPage.length).toBe(1)272 expect(splicedPage[0]).toBe(cachePage1)273 expect(cache.pages.length).toBe(1)274 expect(cache.pages[0]).toBe(cachePage2)275 splicedPage = cache.splicePage(cachePage3) // <--- Different pageNumber from cachePage2276 expect(splicedPage).toBe(null)277 expect(cache.pages.length).toBe(1)278 expect(cache.pages[0]).toBe(cachePage2)279 })280 it ('should send a message when a page is added', (done) => {281 let maxPages = 5282 let cache = new Cache(maxPages)283 let cachePage1 = new CachePage('DATA', 0, 'PARENT', CachePage.NEVER_EXPIRES, null, false)284 let receiver = {285 onMessage: (data) => {286 expect(data.page.data).toBe(cachePage1.data)287 expect(data.pageCount).toBe(1)288 Queue.unregister('TESTING', Messages.CACHE_PAGE_ADDED)289 done()290 }291 }292 Queue.register('TESTING', Messages.CACHE_PAGE_ADDED, (data) => {293 receiver.onMessage(data)294 })295 cache.addPage(cachePage1)296 })297 it ('should send a message when a page is spliced', (done) => {298 let maxPages = 5299 let cache = new Cache(maxPages)300 let cachePage1 = new CachePage('DATA', 0, 'PARENT', CachePage.NEVER_EXPIRES, null, false)301 let cachePage2 = new CachePage('DATA2', 0, 'PARENT', CachePage.NEVER_EXPIRES, null, false)302 let receiver = {303 onMessage: (data) => {304 expect(data.oldPage.data).toBe(cachePage1.data)305 expect(data.newPage.data).toBe(cachePage2.data)306 expect(cachePage2.pageNumber).toBe(data.pageNumber)307 Queue.unregister('TESTING', Messages.CACHE_PAGE_REFRESHED)308 done()309 }310 }311 Queue.register('TESTING', Messages.CACHE_PAGE_REFRESHED, (data) => {312 receiver.onMessage(data)313 })314 cache.addPage(cachePage1)315 cache.splicePage(cachePage2)316 })317 it ('should send a message when a page is removed', (done) => {318 let maxPages = 5319 let cache = new Cache(maxPages)320 let cachePage1 = new CachePage('DATA', 0, 'PARENT', CachePage.NEVER_EXPIRES, null, false)321 let receiver = {322 onMessage: (data) => {323 expect(data.page.data).toBe(cachePage1.data)324 expect(data.pageCount).toBe(0)325 Queue.unregister('TESTING', Messages.CACHE_PAGE_REMOVED)326 done()327 }328 }329 Queue.register('TESTING', Messages.CACHE_PAGE_REMOVED, (data) => {330 receiver.onMessage(data)331 })332 cache.addPage(cachePage1)333 cache.removePage(cachePage1.pageNumber)334 })335 it ('should send a message when the oldest page is removed', (done) => {336 let maxPages = 5337 let cache = new Cache(maxPages)338 let cachePage1 = new CachePage('DATA', 0, 'PARENT', CachePage.NEVER_EXPIRES, null, false)339 let cachePage2 = new CachePage('DATA2', 1, 'PARENT', CachePage.NEVER_EXPIRES, null, false)340 let now = new Date().getTime()341 let receiver = {342 onMessage1: (data) => {343 expect(data.page.data).toBe(cachePage1.data)344 expect(data.pageCount).toBe(1)345 Queue.unregister('TESTING', Messages.CACHE_PAGE_REMOVED)346 done()347 }348 }349 Queue.register('TESTING', Messages.CACHE_PAGE_REMOVED, (data) => {350 receiver.onMessage1(data)351 })352 cache.addPage(cachePage1)353 cache.addPage(cachePage2)354 cachePage1.createdAt = now + 10355 cachePage2.createdAt = now + 20356 cache.removeOldestPage(cachePage2.pageNumber)357 })358 it ('should send a message when the furthest page is removed', (done) => {359 let maxPages = 5360 let cache = new Cache(maxPages)361 let cachePage1 = new CachePage('DATA', 0, 'PARENT', CachePage.NEVER_EXPIRES, null, false)362 let cachePage2 = new CachePage('DATA', 1, 'PARENT', CachePage.NEVER_EXPIRES, null, false)363 let receiver = {364 onMessage1: (data) => {365 expect(data.page.data).toBe(cachePage2.data)366 expect(data.pageCount).toBe(1)367 Queue.unregister('TESTING', Messages.CACHE_PAGE_REMOVED)368 done()369 }370 }371 Queue.register('TESTING', Messages.CACHE_PAGE_REMOVED, (data) => {372 receiver.onMessage1(data)373 })374 cache.addPage(cachePage1)375 cache.addPage(cachePage2)376 cache.removeFurthestPage(cachePage1.pageNumber)377 })378 it ('should send a message when the object is cleared', (done) => {379 let maxPages = 5380 let cache = new Cache(maxPages)381 let cachePage1 = new CachePage('DATA', 0, 'PARENT', CachePage.NEVER_EXPIRES, null, false)382 let cachePage2 = new CachePage('DATA', 1, 'PARENT', CachePage.NEVER_EXPIRES, null, false)383 let receiver = {384 onMessage1: (data) => {385 expect(data.pageCount).toBe(0)386 Queue.unregister('TESTING', Messages.CACHE_CLEARED)387 done()388 }389 }390 Queue.register('TESTING', Messages.CACHE_CLEARED, (data) => {391 receiver.onMessage1(data)392 })393 cache.addPage(cachePage1)394 cache.addPage(cachePage2)395 cache.clear()396 })...
app.js
Source:app.js
1import {otherRouter, appRouter} from '@/router/router';2import Util from '@/libs/util';3import Cookies from 'js-cookie';4import Vue from 'vue';5const app = {6 state: {7 cachePage: [],8 lang: '',9 isFullScreen: false,10 openedSubmenuArr: [], // è¦å±å¼çèåæ°ç»11 menuTheme: 'dark', // 主é¢12 themeColor: '',13 pageOpenedList: [{14 title: 'é¦é¡µ',15 path: '',16 name: 'home_index'17 }],18 currentPageName: '',19 currentPath: [20 {21 title: 'é¦é¡µ',22 path: '',23 name: 'home_index'24 }25 ], // é¢å
å±æ°ç»26 menuList: [],27 routers: [28 otherRouter,29 ...appRouter30 ],31 tagsList: [...otherRouter.children],32 messageCount: 0,33 dontCache: ['text-editor', 'artical-publish'] // å¨è¿éå®ä¹ä½ ä¸æ³è¦ç¼åç页é¢çnameå±æ§å¼(åè§è·¯ç±é
ç½®router.js)34 },35 mutations: {36 setTagsList (state, list) {37 state.tagsList.push(...list);38 },39 updateMenulist (state) {40 //let accessCode = parseInt(Cookies.get('access'));41 let urls = Cookies.get('access').split(",");42 let accessCode = urls;43 let menuList = [];44 appRouter.forEach((item, index) => {45 if (item.access !== undefined) {46 if (Util.showThisRoute(item.access, accessCode)) {47 if (item.children.length === 1) {48 menuList.push(item);49 } else {50 let len = menuList.push(item);51 let childrenArr = [];52 childrenArr = item.children.filter(child => {53 if (child.access !== undefined) {54 if (child.access === accessCode) {55 return child;56 }57 } else {58 return child;59 }60 });61 menuList[len - 1].children = childrenArr;62 }63 }64 } else {65 if (item.children.length === 1) {66 menuList.push(item);67 } else {68 let len = menuList.push(item);69 let childrenArr = [];70 childrenArr = item.children.filter(child => {71 if (child.access !== undefined) {72 if (Util.showThisRoute(child.access, accessCode)) {73 return child;74 }75 } else {76 return child;77 }78 });79 if (childrenArr === undefined || childrenArr.length === 0) {80 menuList.splice(len - 1, 1);81 } else {82 let handledItem = JSON.parse(JSON.stringify(menuList[len - 1]));83 handledItem.children = childrenArr;84 menuList.splice(len - 1, 1, handledItem);85 }86 }87 }88 });89 state.menuList = menuList;90 },91 changeMenuTheme (state, theme) {92 state.menuTheme = theme;93 },94 changeMainTheme (state, mainTheme) {95 state.themeColor = mainTheme;96 },97 addOpenSubmenu (state, name) {98 let hasThisName = false;99 let isEmpty = false;100 if (name.length === 0) {101 isEmpty = true;102 }103 if (state.openedSubmenuArr.indexOf(name) > -1) {104 hasThisName = true;105 }106 if (!hasThisName && !isEmpty) {107 state.openedSubmenuArr.push(name);108 }109 },110 closePage (state, name) {111 state.cachePage.forEach((item, index) => {112 if (item === name) {113 state.cachePage.splice(index, 1);114 }115 });116 },117 initCachepage (state) {118 if (localStorage.cachePage) {119 state.cachePage = JSON.parse(localStorage.cachePage);120 }121 },122 removeTag (state, name) {123 state.pageOpenedList.map((item, index) => {124 if (item.name === name) {125 state.pageOpenedList.splice(index, 1);126 }127 });128 },129 pageOpenedList (state, get) {130 let openedPage = state.pageOpenedList[get.index];131 if (get.argu) {132 openedPage.argu = get.argu;133 }134 if (get.query) {135 openedPage.query = get.query;136 }137 state.pageOpenedList.splice(get.index, 1, openedPage);138 localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList);139 },140 clearAllTags (state) {141 state.pageOpenedList.splice(1);142 state.cachePage.length = 0;143 localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList);144 },145 clearOtherTags (state, vm) {146 let currentName = vm.$route.name;147 let currentIndex = 0;148 state.pageOpenedList.forEach((item, index) => {149 if (item.name === currentName) {150 currentIndex = index;151 }152 });153 if (currentIndex === 0) {154 state.pageOpenedList.splice(1);155 } else {156 state.pageOpenedList.splice(currentIndex + 1);157 state.pageOpenedList.splice(1, currentIndex - 1);158 }159 let newCachepage = state.cachePage.filter(item => {160 return item === currentName;161 });162 state.cachePage = newCachepage;163 localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList);164 },165 setOpenedList (state) {166 state.pageOpenedList = localStorage.pageOpenedList ? JSON.parse(localStorage.pageOpenedList) : [otherRouter.children[0]];167 },168 setCurrentPath (state, pathArr) {169 state.currentPath = pathArr;170 },171 setCurrentPageName (state, name) {172 state.currentPageName = name;173 },174 setAvator (state, path) {175 localStorage.avatorImgPath = path;176 },177 switchLang (state, lang) {178 state.lang = lang;179 Vue.config.lang = lang;180 },181 clearOpenedSubmenu (state) {182 state.openedSubmenuArr.length = 0;183 },184 setMessageCount (state, count) {185 state.messageCount = count;186 },187 increateTag (state, tagObj) {188 if (!Util.oneOf(tagObj.name, state.dontCache)) {189 state.cachePage.push(tagObj.name);190 localStorage.cachePage = JSON.stringify(state.cachePage);191 }192 state.pageOpenedList.push(tagObj);193 localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList);194 }195 }196};...
cache-page.test.js
Source:cache-page.test.js
1// @formatter:off2'use strict'3import CachePage from './cache-page'4import Queue from '../queue'5describe('As a developer, I need to object a page of information', function () {6 beforeAll(() => {7 console.log('BEGIN CACHE PAGE TEST ===========================================')8 })9 beforeEach(() => {10 })11 afterEach(() => {12 Queue.unregisterAll()13 })14 afterAll(() => {15 })16 it ('should be be able to create a object page', () => {17 let data = 'TEST DATA'18 let pageNumber = 019 let parentPostId = 'PARENT POST ID'20 let lifetime = 1000 * 3021 let cachePage = new CachePage(data, pageNumber, lifetime)22 expect(cachePage.data).toBe(data)23 expect(cachePage.pageNumber).toBe(pageNumber)24 expect(cachePage.lifetime).toBe(lifetime)25 expect(cachePage.createdAt).not.toBeUndefined()26 expect(cachePage.createdAt).toBeLessThanOrEqual(new Date().getTime())27 expect(cachePage.hasData()).toBe(true)28 })29 it ('should not expire a page with a lifetime of CachePage.NEVER_EXPIRES', () => {30 let data = 'TEST DATA'31 let pageNumber = 032 let lifetime = CachePage.NEVER_EXPIRES33 let cachePage = new CachePage(data, pageNumber, lifetime)34 expect(cachePage.isExpired()).toBe(false)35 })36 it ('should expire when told to do so', () => {37 let data = 'TEST DATA'38 let pageNumber = 039 let lifetime = CachePage.NEVER_EXPIRES40 let cachePage = new CachePage(data, pageNumber, lifetime)41 cachePage.expireNow()42 expect(cachePage.isExpired()).toBe(true)43 expect(cachePage.lifetime).toBe(-1)44 })...
Using AI Code Generation
1$(document).ready(function() {2 $('.best_in_place').best_in_place();3 $('.best_in_place').bind("ajax:success", function() {4 $(this).cachePage();5 });6});7(function($) {8 $.fn.cachePage = function() {9 $.ajax({10 data: {page: window.location.pathname}11 });12 };13})(jQuery);14 Rails.cache.write(params[:page], render_to_string)15 Dir.chdir(Rails.root) do16 Dir["public/assets/**/*"].reject { |f| File.directory?(f) }17Rails.application.config.assets.precompile += %w( application.js application.css )
Using AI Code Generation
1var BestPracticeTest = require('best-practice-test');2var test = new BestPracticeTest();3 if (err) {4 console.log('Error: ' + err);5 } else {6 console.log('Success: ' + data);7 }8});9var BestPracticeTest = require('best-practice-test');10var test = new BestPracticeTest();11 if (err) {12 console.log('Error: ' + err);13 } else {14 console.log('Success: ' + data);15 }16});17var BestPracticeTest = require('best-practice-test');18var test = new BestPracticeTest();19 if (err) {20 console.log('Error: ' + err);21 } else {22 console.log('Success: ' + data);23 }24});25var BestPracticeTest = require('best-practice-test');26var test = new BestPracticeTest();27 if (err) {28 console.log('Error: ' + err);29 } else {30 console.log('Success: ' + data);31 }32});33var BestPracticeTest = require('best-practice-test');34var test = new BestPracticeTest();35 if (err) {36 console.log('Error: ' + err);37 } else {38 console.log('Success: ' + data);39 }40});41var BestPracticeTest = require('best-practice-test');42var test = new BestPracticeTest();43 if (err) {44 console.log('Error: ' + err);45 } else {46 console.log('Success
Using AI Code Generation
1var BestCache = require('best-cache');2var cache = new BestCache({cacheFolder: './cache'});3 console.log(data);4});5var BestCache = require('best-cache');6var cache = new BestCache({cacheFolder: './cache'});7 console.log(data);8});9var BestCache = require('best-cache');10var cache = new BestCache({cacheFolder: './cache'});11 console.log(data);12});13var BestCache = require('best-cache');14var cache = new BestCache({cacheFolder: './cache'});15 console.log(data);16});17var BestCache = require('best-cache');18var cache = new BestCache({cacheFolder: './cache'});19 console.log(data);20});21var BestCache = require('best-cache');22var cache = new BestCache({cacheFolder: './cache'});23 console.log(data);24});25var BestCache = require('best-cache');26var cache = new BestCache({cacheFolder: './cache'});27 console.log(data);28});29var BestCache = require('best-cache');30var cache = new BestCache({cacheFolder: './cache'});31 console.log(data);32});33var BestCache = require('best-cache');
Using AI Code Generation
1$(document).ready(function() {2 BestInPlaceEditor.cachePage();3});4$(document).ready(function() {5 BestInPlaceEditor.restorePage();6});7$(document).ready(function() {8 BestInPlaceEditor.clearPageCache();9});10$(document).ready(function() {11 BestInPlaceEditor.clearPageCache();12});13$(document).ready(function() {14 BestInPlaceEditor.clearPageCache();15});16$(document).ready(function() {17 BestInPlaceEditor.clearPageCache();18});19$(document).ready(function() {20 BestInPlaceEditor.clearPageCache();21});22$(document).ready(function() {23 BestInPlaceEditor.clearPageCache();24});25$(document).ready(function() {26 BestInPlaceEditor.clearPageCache();27});28$(document).ready(function() {29 BestInPlaceEditor.clearPageCache();30});31$(document).ready(function() {32 BestInPlaceEditor.clearPageCache();33});34$(document).ready(function() {35 BestInPlaceEditor.clearPageCache();36});37$(document).ready(function() {38 BestInPlaceEditor.clearPageCache();39});40$(document).ready(function() {
Using AI Code Generation
1BestInPlaceEditor.prototype.cachePage = function() {2 this.cachedPage = this.getHtml();3};4BestInPlaceEditor.prototype.getHtml = function() {5 return $.ajax({6 }).responseText;7};8BestInPlaceEditor.prototype.getHtml = function() {9 return $.ajax({10 }).responseText;11};12BestInPlaceEditor.prototype.activateForm = function() {13 var output, value;14 value = this.getValue();15 this.form = $(this.formEl);16 this.form.submit($.proxy(this.update, this));17 this.form.find("input[type=submit]").hide();18 this.form.find("input[type=button]").hide();19 this.form.find("input[type=reset]").hide();20 this.form.find("input[type=submit]").hide();21 this.form.find("input[type=button]").hide();22 this.form.find("input[type=reset]").hide();23 this.form.find("input[type=submit]").hide();24 this.form.find("input[type=button]").hide();25 this.form.find("input[type=reset]").hide();26 this.form.find("input[type=submit]").hide();27 this.form.find("input[type=button]").hide();28 this.form.find("input[type=reset]").hide();29 this.form.find("input[type=submit]").hide();30 this.form.find("input[type=button]").hide();31 this.form.find("input[type=reset]").hide();32 this.form.find("input[type=submit]").hide();33 this.form.find("input[type=button]").hide();34 this.form.find("input[type=reset]").hide();35 this.form.find("input[type=submit]").hide();36 this.form.find("input[type=button]").hide();37 this.form.find("input[type=reset]").hide();38 this.form.find("input[type=submit]").hide();39 this.form.find("input[type=button]").hide();40 this.form.find("input[type=reset]").hide();41 this.form.find("input[type=submit]").hide();
Using AI Code Generation
1var editor = new BestInPlaceEditor("div");2editor.cachePage();3BestInPlaceEditor.prototype.cachePage = function(){4 var self = this;5};6BestInPlaceEditor.prototype.cachePage = function(){7 var self = this;8 self.cache = true;9};10BestInPlaceEditor.prototype.cachePage = function(){11 var self = this;12 self.cache = true;13 self.cachedPage = $('body').html();14};15BestInPlaceEditor.prototype.cachePage = function(){16 var self = this;17 self.cache = true;18 self.cachedPage = $('body').html();19};20BestInPlaceEditor.prototype.cachePage = function(){21 var self = this;22 self.cache = true;23 self.cachedPage = $('body').html();24 $('body').append("<button>Refresh</button>");25};26BestInPlaceEditor.prototype.cachePage = function(){27 var self = this;28 self.cache = true;29 self.cachedPage = $('body').html();30 $('body').append("<button>Refresh</button>");31};32BestInPlaceEditor.prototype.cachePage = function(){33 var self = this;34 self.cache = true;35 self.cachedPage = $('body').html();36 $('body').append("<button>Refresh</button>");37 $('button').on('click', function(){
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!!