1
1
import { Application } from 'spectron' ;
2
2
import electronPath from 'electron' ; // Require Electron from the binaries included in node_modules.
3
3
import path from 'path' ;
4
+ import fse from 'fs-extra' ;
5
+ import extract from 'extract-zip' ;
6
+ import { buildSignedUserForDatabase } from './constants' ;
4
7
5
- const createApplication = async (
6
- {
7
- showMessageDialogResponse,
8
- showSaveDialogResponse,
9
- showOpenDialogResponse,
10
- showTours,
11
- } = {
8
+ const getFormattedTime = ( ) => {
9
+ const today = new Date ( ) ;
10
+ const y = today . getFullYear ( ) ;
11
+ // JavaScript months are 0-based.
12
+ const m = today . getMonth ( ) + 1 ;
13
+ const d = today . getDate ( ) ;
14
+ const h = today . getHours ( ) ;
15
+ const mi = today . getMinutes ( ) ;
16
+ const s = today . getSeconds ( ) ;
17
+ return `${ y } ${ m } ${ d } _${ h } -${ mi } -${ s } ` ;
18
+ } ;
19
+
20
+ const setUpDatabase = async ( database = buildSignedUserForDatabase ( ) ) => {
21
+ const tmpDatabasePath = path . join ( __dirname , 'tmp' , getFormattedTime ( ) ) ;
22
+ const varFolder = path . join ( tmpDatabasePath , 'var' ) ;
23
+ fse . ensureDirSync ( varFolder ) ;
24
+
25
+ const db = {
26
+ ...database ,
27
+ spaces : [ ] ,
28
+ appInstanceResources : [ ] ,
29
+ actions : [ ] ,
30
+ } ;
31
+
32
+ if ( database ) {
33
+ // add paths data in var
34
+ const spaces = database ?. spaces || [ ] ;
35
+ // eslint-disable-next-line no-restricted-syntax
36
+ for ( const {
37
+ path : spacePath ,
38
+ space,
39
+ appInstanceResources,
40
+ actions,
41
+ } of spaces ) {
42
+ // eslint-disable-next-line no-await-in-loop
43
+ await extract ( path . join ( __dirname , './fixtures/spaces' , spacePath ) , {
44
+ dir : `${ varFolder } /${ space . id } ` ,
45
+ } ) ;
46
+ db . spaces . push ( space ) ;
47
+ db . appInstanceResources = db . appInstanceResources . concat (
48
+ appInstanceResources
49
+ ) ;
50
+ db . actions = db . actions . concat ( actions ) ;
51
+ }
52
+
53
+ const classrooms = database ?. classrooms || [ ] ;
54
+ // eslint-disable-next-line no-restricted-syntax
55
+ for ( const { id } of classrooms ) {
56
+ fse . ensureDirSync ( path . join ( varFolder , id ) ) ;
57
+ }
58
+
59
+ // set db
60
+ fse . writeFileSync ( `${ varFolder } /db.json` , JSON . stringify ( db ) ) ;
61
+ }
62
+
63
+ return tmpDatabasePath ;
64
+ } ;
65
+
66
+ const createApplication = async ( {
67
+ database = buildSignedUserForDatabase ( ) ,
68
+ responses = {
12
69
showMessageDialogResponse : undefined ,
13
70
showSaveDialogResponse : undefined ,
14
71
showOpenDialogResponse : undefined ,
15
72
showTours : 0 ,
16
- }
17
- ) => {
73
+ } ,
74
+ } = { } ) => {
75
+ const {
76
+ showMessageDialogResponse,
77
+ showSaveDialogResponse,
78
+ showOpenDialogResponse,
79
+ showTours,
80
+ } = responses ;
18
81
const env = { NODE_ENV : 'test' , ELECTRON_IS_DEV : 0 , SHOW_TOURS : showTours } ;
19
82
20
83
if ( showMessageDialogResponse !== undefined ) {
@@ -29,6 +92,9 @@ const createApplication = async (
29
92
env . SHOW_OPEN_DIALOG_RESPONSE = showOpenDialogResponse ;
30
93
}
31
94
95
+ // set up database
96
+ const tmpDatabasePath = await setUpDatabase ( database ) ;
97
+
32
98
const app = new Application ( {
33
99
// Your electron path can be any binary
34
100
// i.e for OSX an example path could be '/Applications/MyApp.app/Contents/MacOS/MyApp'
@@ -49,6 +115,7 @@ const createApplication = async (
49
115
// The following line tells spectron to look and use the main.js file
50
116
// and the package.json located 1 level above.
51
117
args : [ path . join ( __dirname , '../public/electron.js' ) ] ,
118
+ chromeDriverArgs : [ `--user-data-dir=${ tmpDatabasePath } ` ] ,
52
119
env,
53
120
} ) ;
54
121
0 commit comments