import './ui_components/app-root' ; import * as accounts from '../model/accounts' ; import * as server from '../model/server' ; import { App , LAST_DISPLAYED_SERVER_STORAGE_KEY } from './app' ; import { FakeCloudAccounts , FakeDigitalOceanAccount , FakeManualServerRepository } from './testing/models' ; import { AppRoot } from './ui_components/app-root' ; import { Region } from '../model/digitalocean' ; ( global as any ) . onUpdateDownloaded = ( ) => { } ; ( global as any ) . bringToFront = ( ) => { } ; beforeEach ( ( ) => { document . body . innerHTML = "<app-root id='appRoot' language='en'></app-root>" ; } ) ; describe ( 'App' , ( ) => { it ( 'shows intro when starting with no manual servers or DigitalOcean token' , async ( ) => { const appRoot = document . getElementById ( 'appRoot' ) as AppRoot ; const app = createTestApp ( appRoot ) ; await app . start ( ) ; expect ( appRoot . currentPage ) . toEqual ( 'intro' ) ; } ) ; it ( 'will not create a manual server with invalid input' , async ( ) => { const appRoot = document . getElementById ( 'appRoot' ) as AppRoot ; const app = createTestApp ( appRoot ) ; await app . start ( ) ; expect ( appRoot . currentPage ) . toEqual ( 'intro' ) ; await expectAsync ( app . createManualServer ( 'bad input' ) ) . toBeRejectedWithError ( ) ; } ) ; it ( 'creates a manual server with valid input' , async ( ) => { const appRoot = document . getElementById ( 'appRoot' ) as AppRoot ; const app = createTestApp ( appRoot ) ; await app . start ( ) ; expect ( appRoot . currentPage ) . toEqual ( 'intro' ) ; await app . createManualServer ( JSON . stringify ( { certSha256 : 'cert' , apiUrl : 'url' } ) ) ; expect ( appRoot . currentPage ) . toEqual ( 'serverView' ) ; } ) ; it ( 'initially shows servers' , async ( ) => { const fakeAccount = new FakeDigitalOceanAccount ( ) ; await fakeAccount . createServer ( new Region ( '_fake-region-id' ) ) ; const cloudAccounts = new FakeCloudAccounts ( fakeAccount ) ; const manualServerRepo = new FakeManualServerRepository ( ) ; await manualServerRepo . addServer ( { certSha256 : 'cert' , apiUrl : 'fake-manual-server-api-url-1' } ) ; await manualServerRepo . addServer ( { certSha256 : 'cert' , apiUrl : 'fake-manual-server-api-url-2' } ) ; const appRoot = document . getElementById ( 'appRoot' ) as AppRoot ; expect ( appRoot . serverList . length ) . toEqual ( 0 ) ; const app = createTestApp ( appRoot , cloudAccounts , manualServerRepo ) ; await app . start ( ) ; const managedServers = await fakeAccount . listServers ( ) ; expect ( managedServers . length ) . toEqual ( 1 ) ; const manualServers = await manualServerRepo . listServers ( ) ; expect ( manualServers . length ) . toEqual ( 2 ) ; await appRoot . getServerView ( '' ) ; const serverList = appRoot . serverList ; console . log ( `managedServers.length: ${ managedServers . length }` ) ; console . log ( `manualServers.length: ${ manualServers . length }` ) ; expect ( serverList . length ) . toEqual ( manualServers . length + managedServers . length ) ; expect ( serverList ) . toContain ( jasmine . objectContaining ( { id : 'fake-manual-server-api-url-1' } ) ) ; expect ( serverList ) . toContain ( jasmine . objectContaining ( { id : 'fake-manual-server-api-url-2' } ) ) ; expect ( serverList ) . toContain ( jasmine . objectContaining ( { id : '_fake-region-id' } ) ) ; } ) ; it ( 'initially shows the last selected server' , async ( ) => { const LAST_DISPLAYED_SERVER_ID = 'fake-manual-server-api-url-1' ; const manualServerRepo = new FakeManualServerRepository ( ) ; const lastDisplayedServer = await manualServerRepo . addServer ( { certSha256 : 'cert' , apiUrl : LAST_DISPLAYED_SERVER_ID } ) ; await manualServerRepo . addServer ( { certSha256 : 'cert' , apiUrl : 'fake-manual-server-api-url-2' } ) ; localStorage . setItem ( 'lastDisplayedServer' , LAST_DISPLAYED_SERVER_ID ) ; const appRoot = document . getElementById ( 'appRoot' ) as AppRoot ; const app = createTestApp ( appRoot , null , manualServerRepo ) ; await app . start ( ) ; expect ( appRoot . currentPage ) . toEqual ( 'serverView' ) ; expect ( appRoot . selectedServerId ) . toEqual ( lastDisplayedServer . getManagementApiUrl ( ) ) ; } ) ; it ( 'shows progress screen once DigitalOcean droplets are created' , async ( ) => { const appRoot = document . getElementById ( 'appRoot' ) as AppRoot ; const cloudAccounts = new FakeCloudAccounts ( new FakeDigitalOceanAccount ( ) ) ; const app = createTestApp ( appRoot , cloudAccounts ) ; await app . start ( ) ; await app . createDigitalOceanServer ( new Region ( '_fake-region-id' ) ) ; expect ( appRoot . currentPage ) . toEqual ( 'serverView' ) ; const view = await appRoot . getServerView ( appRoot . selectedServerId ) ; expect ( view . selectedPage ) . toEqual ( 'progressView' ) ; } ) ; it ( 'shows progress screen when starting with DigitalOcean servers still being created' , async ( ) => { const appRoot = document . getElementById ( 'appRoot' ) as AppRoot ; const fakeAccount = new FakeDigitalOceanAccount ( ) ; const server = await fakeAccount . createServer ( new Region ( '_fake-region-id' ) ) ; const cloudAccounts = new FakeCloudAccounts ( fakeAccount ) ; const app = createTestApp ( appRoot , cloudAccounts , null ) ; localStorage . setItem ( LAST_DISPLAYED_SERVER_STORAGE_KEY , server . getId ( ) ) ; await app . start ( ) ; expect ( appRoot . currentPage ) . toEqual ( 'serverView' ) ; const view = await appRoot . getServerView ( appRoot . selectedServerId ) ; expect ( view . selectedPage ) . toEqual ( 'progressView' ) ; } ) ; } ) ; function createTestApp ( appRoot , cloudAccounts ? , manualServerRepo ? ) { const VERSION = '0.0.1' ; if ( ! cloudAccounts ) { cloudAccounts = new FakeCloudAccounts ( ) ; } if ( ! manualServerRepo ) { manualServerRepo = new FakeManualServerRepository ( ) ; } return new App ( appRoot , VERSION , manualServerRepo , cloudAccounts ) ; }