How to use initState method in Playwright Internal

Best JavaScript code snippet using playwright-internal

Run Playwright Internal automation tests on LambdaTest cloud grid

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

action.js

Source: action.js Github

copy
1const Zero = () => ({type : "ZERO"})
2const One = () => ({type : "ONE"})
3const Two = () => ({type : "TWO"})
4const Three = () => ({type : "THREE"})
5const Four = () => ({type : "FOUR"})
6const Five = () => ({type : "FIVE"})
7const Six = () => ({type : "SIX"})
8const Seven = () => ({type : "SEVEN"})
9const Eight = () => ({type : "EIGHT"})
10const Nine = () => ({type : "NINE"})
11const Plus = () => ({type : "PLUS"})
12const Minus = () => ({type : "MINUS"})
13const Multiply = () => ({type : "MULTIPLY"})
14const Divide = () => ({type : "DIVIDE"})
15const Equals = () => ({type : "EQUALS"})
16const Clear = () => ({type : "CLEAR"})
17
18const initstate = [];
19     
20const Action = (state = initstate, action) => {
21    if(action.type === "ZERO"){
22        console.log(action.type)
23        return (initstate.push(0),[...initstate])
24    }
25    if(action.type === "ONE"){
26        console.log(action.type)
27        return  (initstate.push(1),[...initstate])
28    }
29    if(action.type === "TWO") {
30        console.log(action.type)
31        return  (initstate.push(2),[...initstate])
32    }
33    if(action.type === "THREE") {
34        console.log(action.type)
35        return  (initstate.push(3),[...initstate])
36    }
37    if(action.type === "FOUR") {
38        console.log(action.type)
39        return  (initstate.push(4),[...initstate])
40    }
41    if(action.type === "FIVE") {
42        console.log(action.type)
43        return  (initstate.push(5),[...initstate])
44    }
45    if(action.type === "SIX") {
46        console.log(action.type)
47        return  (initstate.push(6),[...initstate])
48    }
49    if(action.type === "SEVEN") {
50        console.log(action.type)
51        return  (initstate.push(7),[...initstate])
52    }
53    if(action.type === "EIGHT") {
54        console.log(action.type)
55        return  (initstate.push(8),[...initstate])
56    }
57    if(action.type === "NINE") {
58        console.log(action.type)
59        return  (initstate.push(9),[...initstate])
60    }
61    if(action.type === "PLUS") {
62        return  (initstate.push("+"),[...initstate])
63    }
64    if(action.type === "MINUS") {
65        return  (initstate.push("-"),[...initstate])
66    }
67    if(action.type === "MULTIPLY") {
68        return  (initstate.push("*"),[...initstate])
69    }
70    if(action.type === "DIVIDE") {
71        return (initstate.push("/"),[...initstate])
72    }
73    if(action.type === "EQUALS") {
74        return (initstate.push("="),[...initstate]) 
75    }
76    if(action.type === "CLEAR") {
77        return state = 0
78    }
79    return state;
80}
81
82export default Action;
83export {Zero, One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Plus, Minus, Multiply, Divide, Equals, Clear}
Full Screen

module.js

Source: module.js Github

copy
1let module = (function(){
2    let imgs = document.getElementsByClassName('content-main-image'),
3        filter = document.getElementsByClassName('filterSize'),
4        resultApi = document.getElementById('sidebar-filter-title'),
5        spinner = document.getElementById('spinner')
6
7    let initModule,loadImgs,nextTwentyImgs,prevTwentyImgs,getImgs,
8        updateListImg,select,filterName,sortImgs,takeImg
9    let initState = {
10        select:'all',
11        ResultFetch : [],
12        ImagesArray : [],
13        LargeImgs :[],
14        MediumImgs :[],
15        SmallImgs :[],
16        step : 20
17    }
18    loadImgs = ()=>{
19        spinner.classList.toggle('spin-visible')
20        fetch('https://unsplash.it/list')
21        .then(response=>response.json()
22            .then(data=>{
23                initState.ResultFetch = data
24                initState.ImagesArray = initState.ResultFetch
25                resultApi.innerHTML = `API выдал ${data.length} картинок`
26                spinner.classList.toggle('spin-visible')
27                return initState.ImagesArray;
28            })
29            .then(()=>{    
30                for(let i=0;i<20;i++){
31                    let span = document.createElement('span')
32                    span.innerText  = 
33                    `${initState.ImagesArray[i].width} x ${initState.ImagesArray[i].height}
34                     ${initState.ImagesArray[i].author}`
35                    imgs[i].appendChild(span)
36                }  
37                sortImgs()   
38            })
39        )
40    }
41    nextTwentyImgs = function(){
42        let arrImgs = initState.ImagesArray
43        if(arrImgs.length<20)return
44        for(let i=0;i<20;i++){
45            if(arrImgs[initState.step+i]==undefined){
46                imgs[i].getElementsByTagName('span')[0].innerHTML=''
47                continue;
48             }
49            imgs[i].getElementsByTagName('span')[0].innerHTML =  
50            `${arrImgs[initState.step+i].width} x 
51            ${arrImgs[initState.step+i].height} 
52            ${arrImgs[initState.step+i].author}`
53        }
54        initState.step+=20;
55    }
56    prevTwentyImgs = function(){
57        let arrImgs = initState.ImagesArray
58        if(arrImgs.length<20)return
59        if(initState.step!==20){
60            initState.step-=40;
61            for(let i=0;i<20;i++){
62                imgs[i].getElementsByTagName('span')[0].innerHTML =  
63                `${arrImgs[initState.step+i].width} x 
64                ${arrImgs[initState.step+i].height} 
65                ${arrImgs[initState.step+i].author}`
66            }
67            initState.step+=20
68        }
69    }    
70    sortImgs = function(){
71        initState.ResultFetch.map(item=>{
72            item.height>1500&&
73            item.width>1500&&
74            initState.LargeImgs.push(item)   
75            
76            item.height>800&&item.height<1499&&
77            item.width>800&&item.width<1499&&
78            initState.MediumImgs.push(item)   
79            
80            item.height<799&&initState.SmallImgs.push(item)
81        })
82    }          
83    getImgs = function(size,callback){
84            switch(size){
85                case 'large':
86                initState.select='large'
87                initState.ImagesArray=initState.LargeImgs
88                resultApi.innerHTML = `Найдено ${initState.LargeImgs.length} картинок`
89                callback(initState.LargeImgs)
90                break
91                case 'medium':
92                initState.select='medium'
93                initState.ImagesArray=initState.MediumImgs
94                resultApi.innerHTML = `Найдено ${initState.MediumImgs.length} картинок`
95                callback(initState.MediumImgs)
96                break
97                case 'small':
98                initState.select='small'
99                initState.ImagesArray=initState.SmallImgs
100                resultApi.innerHTML = `Найдено ${initState.SmallImgs.length} картинок`
101                callback(initState.SmallImgs)
102                break
103                default: 
104                initState.select='all'
105                initState.ImagesArray=initState.ResultFetch
106                resultApi.innerHTML = `Всего ${initState.ResultFetch.length} картинок`
107                callback(initState.ResultFetch)
108            }  
109            console.log(initState.select)
110    }
111    updateListImg = function(arrImgs){
112        for(let i=0;i<20;i++){
113            if(arrImgs[i]==undefined){
114               imgs[i].getElementsByTagName('span')[0].innerHTML=''
115               continue;
116            }
117            imgs[i].getElementsByTagName('span')[0].innerHTML = 
118            `${arrImgs[i].width} x 
119            ${arrImgs[i].height}
120            ${arrImgs[i].author}`
121        }
122        initState.step = 20;
123    }
124    select = function(e,key){
125        for(let key of filter) key.classList.remove('select')
126        document.getElementById(e.currentTarget.id).classList.add('select')
127    }
128    takeImg = function(e,element){
129        if(element.getElementsByTagName('span')[0].innerHTML=='')return
130        element.classList.toggle('active')
131    }
132    filterName = function(e,callback){
133        e.code='Backspace'?initState.ImagesArray=initState.ResultFetch:''
134        let resultFilter = [],
135            arrForFilter = []
136        let inputFilterName = e.currentTarget
137        //document.getElementById(inputFilterName.id).nextSibling.innerHTML = inputFilterName.value
138        initState.select == 'all' && (arrForFilter=initState.ResultFetch)
139        initState.select == 'large' && (arrForFilter=initState.LargeImgs)
140        initState.select == 'medium' && (arrForFilter=initState.MediumImgs)
141        initState.select == 'small' && (arrForFilter=initState.SmallImgs)
142
143        arrForFilter.forEach(function(element) {
144            element.author.indexOf(inputFilterName.value)!=-1&&resultFilter.push(element)         
145        }, this);
146        callback(resultFilter)
147    }
148    initModule = ()=>{
149        loadImgs()
150        for(let key of filter)  key.addEventListener('click',(e) => select(e,key))
151        for (let key of imgs) key.addEventListener('click',(e)=>takeImg(e,key))
152        document.getElementById('largeImg').addEventListener('click',()=>getImgs('large',updateListImg))
153        document.getElementById('mediumImg').addEventListener('click',()=>getImgs('medium',updateListImg))
154        document.getElementById('smallImg').addEventListener('click',()=>getImgs('small',updateListImg))
155        document.getElementById('allImg').addEventListener('click',()=>getImgs(null,updateListImg))
156        document.getElementById('arrow-prew').addEventListener('click',()=>prevTwentyImgs())
157        document.getElementById('arrow-next').addEventListener('click',()=>nextTwentyImgs())
158        document.getElementById('filterName').addEventListener('keyup',(e)=>filterName(e,updateListImg))
159    }
160    return {
161        initModule
162    }
163}())
Full Screen

index.test.js

Source: index.test.js Github

copy
1import configureMockStore from "redux-mock-store";
2import thunk from "redux-thunk";
3import cloudreveApp, { initState as cloudreveState } from "./index";
4import { initState as viewUpdateState } from "../redux/viewUpdate/reducer";
5import { initState as explorerState } from "../redux/explorer/reducer";
6
7import {
8    setModalsLoading,
9    openLoadingDialog,
10    openGetSourceDialog,
11    openShareDialog,
12    openMoveDialog,
13    navigateUp,
14    navigateTo,
15    drawerToggleAction,
16    changeViewMethod,
17    changeContextMenu,
18    dragAndDrop,
19    setNavigatorLoadingStatus,
20    setNavigatorError,
21    addSelectedTargets,
22    setNavigator,
23    setSelectedTarget,
24    removeSelectedTargets,
25    toggleDaylightMode,
26    applyThemes,
27    openCreateFolderDialog,
28    openRenameDialog,
29    openRemoveDialog,
30    openResaveDialog,
31    setUserPopover,
32    setShareUserPopover,
33    setSiteConfig,
34    openMusicDialog,
35    openRemoteDownloadDialog,
36    openTorrentDownloadDialog,
37    openDecompressDialog,
38    openCompressDialog,
39    openCopyDialog,
40    closeAllModals,
41    toggleSnackbar,
42    setSessionStatus,
43    enableLoadUploader,
44    refreshFileList,
45    searchMyFile,
46    showImgPreivew,
47    refreshStorage,
48    saveFile,
49    setLastSelect,
50    setShiftSelectedIds,
51} from "../actions/index";
52import { changeSubTitle, setSubtitle } from "../redux/viewUpdate/action";
53import {
54    updateFileList,
55    setFileList,
56    setDirList,
57    setSortMethod,
58    changeSortMethod,
59} from "../redux/explorer/action";
60
61const initState = {
62    ...cloudreveState,
63    viewUpdate: viewUpdateState,
64    explorer: explorerState,
65};
66const middlewares = [thunk];
67const mockStore = configureMockStore(middlewares);
68
69describe("index reducer", () => {
70    it("should return the initial state", () => {
71        expect(cloudreveApp(undefined, { type: "@@INIT" })).toEqual(initState);
72    });
73
74    it("should handle redux init", () => {
75        expect(cloudreveApp(undefined, { type: "@@redux/INIT" })).toEqual(
76            initState
77        );
78    });
79
80    it("should handle DRAWER_TOGGLE", () => {
81        const openAction = drawerToggleAction(true);
82        expect(cloudreveApp(initState, openAction)).toEqual({
83            ...initState,
84            viewUpdate: {
85                ...initState.viewUpdate,
86                open: true,
87            },
88        });
89
90        const clossAction = drawerToggleAction(false);
91        expect(cloudreveApp(initState, clossAction)).toEqual({
92            ...initState,
93            viewUpdate: {
94                ...initState.viewUpdate,
95                open: false,
96            },
97        });
98    });
99
100    it("should handle CHANGE_VIEW_METHOD", () => {
101        const action = changeViewMethod("list");
102        expect(cloudreveApp(initState, action)).toEqual({
103            ...initState,
104            viewUpdate: {
105                ...initState.viewUpdate,
106                explorerViewMethod: "list",
107            },
108        });
109    });
110
111    it("should handle SET_SORT_METHOD", () => {
112        const action = setSortMethod("sizeRes");
113        expect(cloudreveApp(initState, action)).toEqual({
114            ...initState,
115            viewUpdate: {
116                ...initState.viewUpdate,
117                sortMethod: "sizeRes",
118            },
119        });
120    });
121
122    describe("CHANGE_SORT_METHOD", () => {
123        const explorerState = {
124            fileList: [
125                {
126                    type: "file",
127                    name: "b",
128                    size: 10,
129                    date: "2020/04/30",
130                },
131                {
132                    type: "file",
133                    name: "a",
134                    size: 11,
135                    date: "2020/05/01",
136                },
137                {
138                    type: "file",
139                    name: "z",
140                    size: 110,
141                    date: "2020/04/29",
142                },
143            ],
144            dirList: [
145                {
146                    type: "dir",
147                    name: "b_dir",
148                    size: 10,
149                    date: "2020/04/30",
150                },
151                {
152                    type: "dir",
153                    name: "a_dir",
154                    size: 11,
155                    date: "2020/05/01",
156                },
157                {
158                    type: "dir",
159                    name: "z_dir",
160                    size: 110,
161                    date: "2020/04/29",
162                },
163            ],
164        };
165
166        const state = {
167            ...initState,
168            explorer: {
169                ...initState.explorer,
170                ...explorerState,
171            },
172        };
173        it("should handle sizePos", async () => {
174            const action = changeSortMethod("sizePos");
175            const sortFunc = (a, b) => {
176                return a.size - b.size;
177            };
178            const fileList = explorerState.fileList;
179            const dirList = explorerState.dirList;
180            const store = mockStore(state);
181            await store.dispatch(action);
182            expect(store.getActions()).toEqual([
183                setSortMethod("sizePos"),
184                setDirList(dirList.sort(sortFunc)),
185                setFileList(fileList.sort(sortFunc)),
186            ]);
187        });
188
189        it("should handle sizeRes", async () => {
190            const action = changeSortMethod("sizePos");
191            const sortFunc = (a, b) => {
192                return b.size - a.size;
193            };
194            const fileList = explorerState.fileList;
195            const dirList = explorerState.dirList;
196            const store = mockStore(state);
197            await store.dispatch(action);
198            expect(store.getActions()).toEqual([
199                setSortMethod("sizePos"),
200                setDirList(dirList.sort(sortFunc)),
201                setFileList(fileList.sort(sortFunc)),
202            ]);
203        });
204
205        it("should handle namePos", async () => {
206            const action = changeSortMethod("namePos");
207            const sortFunc = (a, b) => {
208                return a.name.localeCompare(b.name);
209            };
210            const fileList = explorerState.fileList;
211            const dirList = explorerState.dirList;
212            const store = mockStore(state);
213            await store.dispatch(action);
214            expect(store.getActions()).toEqual([
215                setSortMethod("namePos"),
216                setDirList(dirList.sort(sortFunc)),
217                setFileList(fileList.sort(sortFunc)),
218            ]);
219        });
220
221        it("should handle nameRev", async () => {
222            const action = changeSortMethod("nameRev");
223            const sortFunc = (a, b) => {
224                return b.name.localeCompare(a.name);
225            };
226            const fileList = explorerState.fileList;
227            const dirList = explorerState.dirList;
228            const store = mockStore(state);
229            await store.dispatch(action);
230            expect(store.getActions()).toEqual([
231                setSortMethod("nameRev"),
232                setDirList(dirList.sort(sortFunc)),
233                setFileList(fileList.sort(sortFunc)),
234            ]);
235        });
236
237        it("should handle timePos", async () => {
238            const action = changeSortMethod("timePos");
239            const sortFunc = (a, b) => {
240                return Date.parse(a.date) - Date.parse(b.date);
241            };
242            const fileList = explorerState.fileList;
243            const dirList = explorerState.dirList;
244            const store = mockStore(state);
245            await store.dispatch(action);
246            expect(store.getActions()).toEqual([
247                setSortMethod("timePos"),
248                setDirList(dirList.sort(sortFunc)),
249                setFileList(fileList.sort(sortFunc)),
250            ]);
251        });
252
253        it("should handle timeRev", async () => {
254            const action = changeSortMethod("timeRev");
255            const sortFunc = (a, b) => {
256                return Date.parse(b.date) - Date.parse(a.date);
257            };
258            const fileList = explorerState.fileList;
259            const dirList = explorerState.dirList;
260            const store = mockStore(state);
261            await store.dispatch(action);
262            expect(store.getActions()).toEqual([
263                setSortMethod("timeRev"),
264                setDirList(dirList.sort(sortFunc)),
265                setFileList(fileList.sort(sortFunc)),
266            ]);
267        });
268    });
269
270    it("should handle CHANGE_CONTEXT_MENU", () => {
271        const action1 = changeContextMenu("empty", false);
272        expect(cloudreveApp(initState, action1)).toEqual({
273            ...initState,
274            viewUpdate: {
275                ...initState.viewUpdate,
276                contextOpen: false,
277                contextType: "empty",
278            },
279        });
280        const action2 = changeContextMenu("aa", true);
281        expect(cloudreveApp(initState, action2)).toEqual({
282            ...initState,
283            viewUpdate: {
284                ...initState.viewUpdate,
285                contextOpen: true,
286                contextType: "aa",
287            },
288        });
289    });
290
291    it("should handle DRAG_AND_DROP", () => {
292        const action = dragAndDrop("source", "target");
293        expect(cloudreveApp(initState, action)).toEqual({
294            ...initState,
295            explorer: {
296                ...initState.explorer,
297                dndSignal: true,
298                dndTarget: "target",
299                dndSource: "source",
300            },
301        });
302    });
303
304    it("should handle SET_NAVIGATOR_LOADING_STATUE", () => {
305        const action = setNavigatorLoadingStatus(true);
306        expect(cloudreveApp(initState, action)).toEqual({
307            ...initState,
308            viewUpdate: {
309                ...initState.viewUpdate,
310                navigatorLoading: true,
311            },
312        });
313    });
314
315    it("should handle SET_NAVIGATOR_ERROR", () => {
316        const action = setNavigatorError(true, "Error Message");
317        expect(cloudreveApp(initState, action)).toEqual({
318            ...initState,
319            viewUpdate: {
320                ...initState.viewUpdate,
321                navigatorError: true,
322                navigatorErrorMsg: "Error Message",
323            },
324        });
325    });
326
327    describe("UPDATE_FILE_LIST", () => {
328        const fileList = [
329            {
330                type: "file",
331                name: "b",
332                size: 10,
333                date: "2020/04/30",
334            },
335            {
336                type: "file",
337                name: "a",
338                size: 11,
339                date: "2020/05/01",
340            },
341            {
342                type: "file",
343                name: "z",
344                size: 110,
345                date: "2020/04/29",
346            },
347        ];
348        const dirList = [
349            {
350                type: "dir",
351                name: "b_dir",
352                size: 10,
353                date: "2020/04/30",
354            },
355            {
356                type: "dir",
357                name: "a_dir",
358                size: 11,
359                date: "2020/05/01",
360            },
361            {
362                type: "dir",
363                name: "z_dir",
364                size: 110,
365                date: "2020/04/29",
366            },
367        ];
368        const updateAction = updateFileList([...fileList, ...dirList]);
369        it("should handle sizePos", async () => {
370            const sortFun = (a, b) => {
371                return a.size - b.size;
372            };
373            const state = {
374                ...initState,
375                viewUpdate: {
376                    ...initState.viewUpdate,
377                    sortMethod: "sizePos",
378                },
379            };
380            const store = mockStore(state);
381            await store.dispatch(updateAction);
382            expect(store.getActions()).toEqual([
383                setDirList(dirList.sort(sortFun)),
384                setFileList(fileList.sort(sortFun)),
385            ]);
386        });
387
388        it("should handle sizeRes", async () => {
389            const sortFun = (a, b) => {
390                return b.size - a.size;
391            };
392            const state = {
393                ...initState,
394                viewUpdate: {
395                    ...initState.viewUpdate,
396                    sortMethod: "sizeRes",
397                },
398            };
399            const store = mockStore(state);
400            await store.dispatch(updateAction);
401            expect(store.getActions()).toEqual([
402                setDirList(dirList.sort(sortFun)),
403                setFileList(fileList.sort(sortFun)),
404            ]);
405        });
406
407        it("should handle namePos", async () => {
408            const sortFun = (a, b) => {
409                return a.name.localeCompare(b.name);
410            };
411            const state = {
412                ...initState,
413                viewUpdate: {
414                    ...initState.viewUpdate,
415                    sortMethod: "namePos",
416                },
417            };
418            const store = mockStore(state);
419            await store.dispatch(updateAction);
420            expect(store.getActions()).toEqual([
421                setDirList(dirList.sort(sortFun)),
422                setFileList(fileList.sort(sortFun)),
423            ]);
424        });
425
426        it("should handle nameRev", async () => {
427            const sortFun = (a, b) => {
428                return b.name.localeCompare(a.name);
429            };
430            const state = {
431                ...initState,
432                viewUpdate: {
433                    ...initState.viewUpdate,
434                    sortMethod: "nameRev",
435                },
436            };
437            const store = mockStore(state);
438            await store.dispatch(updateAction);
439            expect(store.getActions()).toEqual([
440                setDirList(dirList.sort(sortFun)),
441                setFileList(fileList.sort(sortFun)),
442            ]);
443        });
444
445        it("should handle timePos", async () => {
446            const sortFun = (a, b) => {
447                return Date.parse(a.date) - Date.parse(b.date);
448            };
449            const state = {
450                ...initState,
451                viewUpdate: {
452                    ...initState.viewUpdate,
453                    sortMethod: "timePos",
454                },
455            };
456            const store = mockStore(state);
457            await store.dispatch(updateAction);
458            expect(store.getActions()).toEqual([
459                setDirList(dirList.sort(sortFun)),
460                setFileList(fileList.sort(sortFun)),
461            ]);
462        });
463
464        it("should handle timeRev", async () => {
465            const sortFun = (a, b) => {
466                return Date.parse(b.date) - Date.parse(a.date);
467            };
468            const state = {
469                ...initState,
470                viewUpdate: {
471                    ...initState.viewUpdate,
472                    sortMethod: "timeRev",
473                },
474            };
475            const store = mockStore(state);
476            await store.dispatch(updateAction);
477            expect(store.getActions()).toEqual([
478                setDirList(dirList.sort(sortFun)),
479                setFileList(fileList.sort(sortFun)),
480            ]);
481        });
482    });
483
484    it("should handle SET_FILE_LIST", () => {
485        const action = setFileList([
486            {
487                type: "file",
488                id: "a",
489            },
490            {
491                type: "file",
492                id: "b",
493            },
494        ]);
495        expect(
496            cloudreveApp(
497                {
498                    ...initState,
499                    explorer: {
500                        ...initState.explorer,
501                        fileList: [{ type: "file", id: "test" }],
502                    },
503                },
504                action
505            )
506        ).toEqual({
507            ...initState,
508            explorer: {
509                ...initState.explorer,
510                fileList: [
511                    {
512                        type: "file",
513                        id: "a",
514                    },
515                    {
516                        type: "file",
517                        id: "b",
518                    },
519                ],
520            },
521        });
522    });
523
524    it("should handle SET_DIR_LIST", () => {
525        const action = setDirList([
526            {
527                type: "dir",
528                id: "a",
529            },
530            {
531                type: "dir",
532                id: "b",
533            },
534        ]);
535        expect(
536            cloudreveApp(
537                {
538                    ...initState,
539                    explorer: {
540                        ...initState.explorer,
541                        dirList: [{ type: "dir", id: "test" }],
542                    },
543                },
544                action
545            )
546        ).toEqual({
547            ...initState,
548            explorer: {
549                ...initState.explorer,
550                dirList: [
551                    {
552                        type: "dir",
553                        id: "a",
554                    },
555                    {
556                        type: "dir",
557                        id: "b",
558                    },
559                ],
560            },
561        });
562    });
563
564    it("should handle ADD_SELECTED_TARGETS", () => {
565        const newSelect = [
566            {
567                type: "file",
568            },
569            {
570                type: "dir",
571            },
572        ];
573        const action = addSelectedTargets(newSelect);
574        expect(
575            cloudreveApp(
576                {
577                    ...initState,
578                    explorer: {
579                        ...initState.explorer,
580                        selected: [{ type: "file" }],
581                    },
582                },
583                action
584            )
585        ).toEqual({
586            ...initState,
587            explorer: {
588                ...initState.explorer,
589                selected: [{ type: "file" }, ...newSelect],
590                selectProps: {
591                    isMultiple: true,
592                    withFolder: true,
593                    withFile: true,
594                },
595            },
596        });
597    });
598
599    it("should handle SET_SELECTED_TARGET", () => {
600        const newSelect = [
601            {
602                type: "file",
603            },
604            {
605                type: "dir",
606            },
607        ];
608        const action = setSelectedTarget(newSelect);
609        expect(
610            cloudreveApp(
611                {
612                    ...initState,
613                    explorer: {
614                        ...initState.explorer,
615                        selected: [{ type: "file" }],
616                    },
617                },
618                action
619            )
620        ).toEqual({
621            ...initState,
622            explorer: {
623                ...initState.explorer,
624                selected: newSelect,
625                selectProps: {
626                    isMultiple: true,
627                    withFolder: true,
628                    withFile: true,
629                },
630            },
631        });
632    });
633
634    it("should handle RMOVE_SELECTED_TARGETS", () => {
635        const remove = ["1"];
636        const action = removeSelectedTargets(remove);
637        expect(
638            cloudreveApp(
639                {
640                    ...initState,
641                    explorer: {
642                        ...initState.explorer,
643                        selected: [
644                            { id: "1", type: "file" },
645                            { id: "2", type: "file" },
646                        ],
647                    },
648                },
649                action
650            )
651        ).toEqual({
652            ...initState,
653            explorer: {
654                ...initState.explorer,
655                selected: [{ id: "2", type: "file" }],
656                selectProps: {
657                    isMultiple: false,
658                    withFolder: false,
659                    withFile: true,
660                },
661            },
662        });
663    });
664
665    it("should handle NAVIGATOR_TO", async () => {
666        const store = mockStore(initState);
667        const action = navigateTo("/somewhere");
668        await store.dispatch(action);
669        expect(store.getActions()).toEqual([setNavigator("/somewhere", true)]);
670    });
671
672    it("should handle NAVIGATOR_UP", async () => {
673        const navState = {
674            ...initState,
675            navigator: {
676                ...initState.navigator,
677                path: "/to/somewhere",
678            },
679        };
680        const store = mockStore(navState);
681        const action = navigateUp();
682        await store.dispatch(action);
683        expect(store.getActions()).toEqual([setNavigator("/to", true)]);
684    });
685
686    it("should handle SET_NAVIGATOR", () => {
687        const navState = {
688            ...initState,
689            navigator: {
690                ...initState.navigator,
691                path: "/to/somewhere",
692            },
693        };
694        const action = setNavigator("/newpath", true);
695        expect(cloudreveApp(navState, action)).toEqual({
696            ...initState,
697            navigator: {
698                ...initState.navigator,
699                path: "/newpath",
700            },
701            viewUpdate: {
702                ...initState.viewUpdate,
703                contextOpen: false,
704                navigatorError: false,
705                navigatorLoading: true,
706            },
707            explorer: {
708                ...initState.explorer,
709                selected: [],
710                selectProps: {
711                    isMultiple: false,
712                    withFolder: false,
713                    withFile: false,
714                },
715                keywords: "",
716            },
717        });
718        expect(window.currntPath).toEqual("/newpath");
719    });
720
721    it("should handle TOGGLE_DAYLIGHT_MODE", () => {
722        const action = toggleDaylightMode();
723        const darkState = {
724            ...initState,
725            siteConfig: {
726                ...initState.siteConfig,
727                theme: {
728                    ...initState.siteConfig.theme,
729                    palette: {
730                        ...initState.siteConfig.theme.palette,
731                        type: "dark",
732                    },
733                },
734            },
735        };
736        const lightState = {
737            ...initState,
738            siteConfig: {
739                ...initState.siteConfig,
740                theme: {
741                    ...initState.siteConfig.theme,
742                    palette: {
743                        ...initState.siteConfig.theme.palette,
744                        type: "light",
745                    },
746                },
747            },
748        };
749        expect(cloudreveApp(initState, action)).toEqual(darkState);
750        expect(cloudreveApp(darkState, action)).toEqual(lightState);
751    });
752
753    it("should handle APPLY_THEME", () => {
754        const action = applyThemes("foo");
755        const stateWithThemes = {
756            ...initState,
757            siteConfig: {
758                ...initState.siteConfig,
759                themes: JSON.stringify({ foo: "bar" }),
760            },
761        };
762        expect(cloudreveApp(stateWithThemes, action)).toEqual({
763            ...stateWithThemes,
764            siteConfig: {
765                ...stateWithThemes.siteConfig,
766                theme: "bar",
767            },
768        });
769    });
770
771    it("should handle OPEN_CREATE_FOLDER_DIALOG", () => {
772        const action = openCreateFolderDialog();
773        expect(cloudreveApp(initState, action)).toEqual({
774            ...initState,
775            viewUpdate: {
776                ...initState.viewUpdate,
777                modals: {
778                    ...initState.viewUpdate.modals,
779                    createNewFolder: true,
780                },
781                contextOpen: false,
782            },
783        });
784    });
785
786    it("should handle OPEN_RENAME_DIALOG", () => {
787        const action = openRenameDialog();
788        expect(cloudreveApp(initState, action)).toEqual({
789            ...initState,
790            viewUpdate: {
791                ...initState.viewUpdate,
792                modals: {
793                    ...initState.viewUpdate.modals,
794                    rename: true,
795                },
796                contextOpen: false,
797            },
798        });
799    });
800
801    it("should handle OPEN_REMOVE_DIALOG", () => {
802        const action = openRemoveDialog();
803        expect(cloudreveApp(initState, action)).toEqual({
804            ...initState,
805            viewUpdate: {
806                ...initState.viewUpdate,
807                modals: {
808                    ...initState.viewUpdate.modals,
809                    remove: true,
810                },
811                contextOpen: false,
812            },
813        });
814    });
815
816    it("should handle OPEN_MOVE_DIALOG", () => {
817        const action = openMoveDialog();
818        expect(cloudreveApp(initState, action)).toEqual({
819            ...initState,
820            viewUpdate: {
821                ...initState.viewUpdate,
822                modals: {
823                    ...initState.viewUpdate.modals,
824                    move: true,
825                },
826                contextOpen: false,
827            },
828        });
829    });
830
831    it("should handle OPEN_RESAVE_DIALOG", () => {
832        const action = openResaveDialog();
833        expect(cloudreveApp(initState, action)).toEqual({
834            ...initState,
835            viewUpdate: {
836                ...initState.viewUpdate,
837                modals: {
838                    ...initState.viewUpdate.modals,
839                    resave: true,
840                },
841                contextOpen: false,
842            },
843        });
844    });
845
846    it("should handle SET_USER_POPOVER", () => {
847        // TODO: update to real anchor
848        const action = setUserPopover("anchor");
849        expect(cloudreveApp(initState, action)).toEqual({
850            ...initState,
851            viewUpdate: {
852                ...initState.viewUpdate,
853                userPopoverAnchorEl: "anchor",
854            },
855        });
856    });
857
858    it("should handle SET_SHARE_USER_POPOVER", () => {
859        // TODO: update to real anchor
860        const action = setShareUserPopover("anchor");
861        expect(cloudreveApp(initState, action)).toEqual({
862            ...initState,
863            viewUpdate: {
864                ...initState.viewUpdate,
865                shareUserPopoverAnchorEl: "anchor",
866            },
867        });
868    });
869
870    it("should handle OPEN_SHARE_DIALOG", () => {
871        // TODO: update to real anchor
872        const action = openShareDialog();
873        expect(cloudreveApp(initState, action)).toEqual({
874            ...initState,
875            viewUpdate: {
876                ...initState.viewUpdate,
877                modals: {
878                    ...initState.viewUpdate.modals,
879                    share: true,
880                },
881                contextOpen: false,
882            },
883        });
884    });
885
886    it("should handle SET_SITE_CONFIG", () => {
887        // TODO: update to real anchor
888        const action = setSiteConfig({ foo: "bar" });
889        expect(cloudreveApp(initState, action)).toEqual({
890            ...initState,
891            siteConfig: {
892                foo: "bar",
893            },
894        });
895    });
896
897    it("should handle SET_SITE_CONFIG", () => {
898        // TODO: update to real anchor
899        const action = setSiteConfig({ foo: "bar" });
900        expect(cloudreveApp(initState, action)).toEqual({
901            ...initState,
902            siteConfig: {
903                foo: "bar",
904            },
905        });
906    });
907
908    it("should handle OPEN_MUSIC_DIALOG", () => {
909        const action = openMusicDialog();
910        expect(cloudreveApp(initState, action)).toEqual({
911            ...initState,
912            viewUpdate: {
913                ...initState.viewUpdate,
914                modals: {
915                    ...initState.viewUpdate.modals,
916                    music: true,
917                },
918                contextOpen: false,
919            },
920        });
921    });
922
923    it("should handle OPEN_REMOTE_DOWNLOAD_DIALOG", () => {
924        const action = openRemoteDownloadDialog();
925        expect(cloudreveApp(initState, action)).toEqual({
926            ...initState,
927            viewUpdate: {
928                ...initState.viewUpdate,
929                modals: {
930                    ...initState.viewUpdate.modals,
931                    remoteDownload: true,
932                },
933                contextOpen: false,
934            },
935        });
936    });
937
938    it("should handle OPEN_TORRENT_DOWNLOAD_DIALOG", () => {
939        const action = openTorrentDownloadDialog();
940        expect(cloudreveApp(initState, action)).toEqual({
941            ...initState,
942            viewUpdate: {
943                ...initState.viewUpdate,
944                modals: {
945                    ...initState.viewUpdate.modals,
946                    torrentDownload: true,
947                },
948                contextOpen: false,
949            },
950        });
951    });
952
953    it("should handle OPEN_DECOMPRESS_DIALOG", () => {
954        const action = openDecompressDialog();
955        expect(cloudreveApp(initState, action)).toEqual({
956            ...initState,
957            viewUpdate: {
958                ...initState.viewUpdate,
959                modals: {
960                    ...initState.viewUpdate.modals,
961                    decompress: true,
962                },
963                contextOpen: false,
964            },
965        });
966    });
967
968    it("should handle OPEN_COMPRESS_DIALOG", () => {
969        const action = openCompressDialog();
970        expect(cloudreveApp(initState, action)).toEqual({
971            ...initState,
972            viewUpdate: {
973                ...initState.viewUpdate,
974                modals: {
975                    ...initState.viewUpdate.modals,
976                    compress: true,
977                },
978                contextOpen: false,
979            },
980        });
981    });
982
983    it("should handle OPEN_GET_SOURCE_DIALOG", () => {
984        const action = openGetSourceDialog();
985        expect(cloudreveApp(initState, action)).toEqual({
986            ...initState,
987            viewUpdate: {
988                ...initState.viewUpdate,
989                modals: {
990                    ...initState.viewUpdate.modals,
991                    getSource: true,
992                },
993                contextOpen: false,
994            },
995        });
996    });
997
998    it("should handle OPEN_COPY_DIALOG", () => {
999        const action = openCopyDialog();
1000        expect(cloudreveApp(initState, action)).toEqual({
1001            ...initState,
1002            viewUpdate: {
1003                ...initState.viewUpdate,
1004                modals: {
1005                    ...initState.viewUpdate.modals,
1006                    copy: true,
1007                },
1008                contextOpen: false,
1009            },
1010        });
1011    });
1012
1013    it("should handle OPEN_LOADING_DIALOG", () => {
1014        const action = openLoadingDialog("loading");
1015        expect(cloudreveApp(initState, action)).toEqual({
1016            ...initState,
1017            viewUpdate: {
1018                ...initState.viewUpdate,
1019                modals: {
1020                    ...initState.viewUpdate.modals,
1021                    loading: true,
1022                    loadingText: "loading",
1023                },
1024                contextOpen: false,
1025            },
1026        });
1027    });
1028
1029    it("should handle CLOSE_ALL_MODALS", () => {
1030        const action = closeAllModals();
1031        expect(cloudreveApp(initState, action)).toEqual({
1032            ...initState,
1033            viewUpdate: {
1034                ...initState.viewUpdate,
1035                modals: {
1036                    ...initState.viewUpdate.modals,
1037                    createNewFolder: false,
1038                    rename: false,
1039                    move: false,
1040                    remove: false,
1041                    share: false,
1042                    music: false,
1043                    remoteDownload: false,
1044                    torrentDownload: false,
1045                    getSource: false,
1046                    resave: false,
1047                    copy: false,
1048                    loading: false,
1049                    compress: false,
1050                    decompress: false,
1051                },
1052            },
1053        });
1054    });
1055
1056    it("should handle CHANGE_SUB_TITLE", async () => {
1057        const store = mockStore(initState);
1058        const action = changeSubTitle("test sub title");
1059        await store.dispatch(action);
1060        expect(store.getActions()).toEqual([setSubtitle("test sub title")]);
1061        expect(document.title).toEqual("test sub title - CloudEnterprise");
1062    });
1063
1064    it("should handle SET_SUBTITLE", () => {
1065        const action = setSubtitle("test sub title 2");
1066        expect(cloudreveApp(initState, action)).toEqual({
1067            ...initState,
1068            viewUpdate: {
1069                ...initState.viewUpdate,
1070                subTitle: "test sub title 2",
1071            },
1072        });
1073    });
1074
1075    it("should handle TOGGLE_SNACKBAR", () => {
1076        const action = toggleSnackbar(
1077            "top",
1078            "right",
1079            "something wrong",
1080            "error"
1081        );
1082        expect(cloudreveApp(initState, action)).toEqual({
1083            ...initState,
1084            viewUpdate: {
1085                ...initState.viewUpdate,
1086                snackbar: {
1087                    toggle: true,
1088                    vertical: "top",
1089                    horizontal: "right",
1090                    msg: "something wrong",
1091                    color: "error",
1092                },
1093            },
1094        });
1095    });
1096
1097    it("should handle SET_MODALS_LOADING", () => {
1098        const action = setModalsLoading("test loading status");
1099        expect(cloudreveApp(initState, action)).toEqual({
1100            ...initState,
1101            viewUpdate: {
1102                ...initState.viewUpdate,
1103                modalsLoading: "test loading status",
1104            },
1105        });
1106    });
1107
1108    it("should handle SET_SESSION_STATUS", () => {
1109        const action = setSessionStatus(true);
1110        expect(cloudreveApp(initState, action)).toEqual({
1111            ...initState,
1112            viewUpdate: {
1113                ...initState.viewUpdate,
1114                isLogin: true,
1115            },
1116        });
1117    });
1118
1119    it("should handle ENABLE_LOAD_UPLOADER", () => {
1120        const action = enableLoadUploader();
1121        expect(cloudreveApp(initState, action)).toEqual({
1122            ...initState,
1123            viewUpdate: {
1124                ...initState.viewUpdate,
1125                loadUploader: true,
1126            },
1127        });
1128    });
1129
1130    it("should handle REFRESH_FILE_LIST", () => {
1131        const action = refreshFileList();
1132        expect(cloudreveApp(initState, action)).toEqual({
1133            ...initState,
1134            navigator: {
1135                ...initState.navigator,
1136                refresh: false,
1137            },
1138            explorer: {
1139                ...initState.explorer,
1140                selected: [],
1141                selectProps: {
1142                    isMultiple: false,
1143                    withFolder: false,
1144                    withFile: false,
1145                },
1146            },
1147        });
1148    });
1149
1150    it("should handle SEARCH_MY_FILE", () => {
1151        const action = searchMyFile("keyword");
1152        expect(cloudreveApp(initState, action)).toEqual({
1153            ...initState,
1154            navigator: {
1155                ...initState.navigator,
1156                path: "/搜索结果",
1157                refresh: true,
1158            },
1159            viewUpdate: {
1160                ...initState.viewUpdate,
1161                contextOpen: false,
1162                navigatorError: false,
1163                navigatorLoading: true,
1164            },
1165            explorer: {
1166                ...initState.explorer,
1167                selected: [],
1168                selectProps: {
1169                    isMultiple: false,
1170                    withFolder: false,
1171                    withFile: false,
1172                },
1173                keywords: "keyword",
1174            },
1175        });
1176    });
1177
1178    it("should handle SHOW_IMG_PREIVEW", () => {
1179        const action = showImgPreivew({ type: "file" });
1180        const showImgState = {
1181            ...initState,
1182            explorer: {
1183                ...initState.explorer,
1184                fileList: [{ type: "file" }, { type: "dir" }],
1185            },
1186        };
1187        expect(cloudreveApp(showImgState, action)).toEqual({
1188            ...showImgState,
1189            explorer: {
1190                ...showImgState.explorer,
1191                imgPreview: {
1192                    ...showImgState.explorer.imgPreview,
1193                    first: { type: "file" },
1194                    other: [{ type: "file" }, { type: "dir" }],
1195                },
1196            },
1197        });
1198    });
1199
1200    it("should handle REFRESH_STORAGE", () => {
1201        const action = refreshStorage();
1202
1203        expect(cloudreveApp(initState, action)).toEqual({
1204            ...initState,
1205            viewUpdate: {
1206                ...initState.viewUpdate,
1207                storageRefresh: true,
1208            },
1209        });
1210    });
1211
1212    it("should handle SAVE_FILE", () => {
1213        const action = saveFile();
1214        expect(cloudreveApp(initState, action)).toEqual({
1215            ...initState,
1216            explorer: {
1217                ...initState.explorer,
1218                fileSave: true,
1219            },
1220        });
1221    });
1222
1223    it("should handle SET_LAST_SELECT", () => {
1224        const action = setLastSelect({ type: "file" }, 1);
1225        expect(cloudreveApp(initState, action)).toEqual({
1226            ...initState,
1227            explorer: {
1228                ...initState.explorer,
1229                lastSelect: {
1230                    file: { type: "file" },
1231                    index: 1,
1232                },
1233            },
1234        });
1235    });
1236
1237    it("should handle SET_SHIFT_SELECTED_IDS", () => {
1238        const action = setShiftSelectedIds(["1", "2"]);
1239        expect(cloudreveApp(initState, action)).toEqual({
1240            ...initState,
1241            explorer: {
1242                ...initState.explorer,
1243                shiftSelectedIds: ["1", "2"],
1244            },
1245        });
1246    });
1247});
1248
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Internal on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)