/**
 * Copyright 2020 Gravitational, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ import renderHook from 'design/utils/renderHook' ; import ConsoleContext from './consoleContext' ; import useOnExitConfirmation from './useOnExitConfirmation' ; import session from 'teleport/services/session' ; test ( 'confirmation dialog before terminating an active ssh session' , ( ) => { const ctx = new ConsoleContext ( ) ; const { current } = renderHook ( ( ) => useOnExitConfirmation ( ctx ) ) ; const event = new Event ( 'beforeunload' ) ; jest . spyOn ( window , 'confirm' ) . mockReturnValue ( false ) ; jest . spyOn ( event , 'preventDefault' ) ; ctx . storeDocs . add ( { kind : 'nodes' , url : 'test' , created : new Date ( ) , } ) ; let docs = ctx . getDocuments ( ) ; const docBlank = docs [ 0 ] ; let retVal = current . verifyAndConfirm ( docBlank ) ; expect ( retVal ) . toBe ( true ) ; expect ( window . confirm ) . not . toHaveBeenCalled ( ) ; const docNode = docs [ 1 ] ; retVal = current . verifyAndConfirm ( docNode ) ; expect ( retVal ) . toBe ( true ) ; expect ( window . confirm ) . not . toHaveBeenCalled ( ) ; window . dispatchEvent ( event ) ; expect ( event . preventDefault ) . not . toHaveBeenCalled ( ) ; ctx . storeDocs . add ( { kind : 'terminal' , status : 'connected' , url : 'localhost' , serverId : 'serverId' , login : 'login' , created : new Date ( ) , sid : 'random-123-sid' , } ) ; docs = ctx . getDocuments ( ) ; const docTerminal = docs [ 2 ] ; retVal = current . verifyAndConfirm ( docTerminal ) ; expect ( retVal ) . toBe ( true ) ; expect ( window . confirm ) . not . toHaveBeenCalled ( ) ; window . dispatchEvent ( event ) ; expect ( event . preventDefault ) . not . toHaveBeenCalled ( ) ; docTerminal . created = new Date ( '2019-04-01' ) ; jest . spyOn ( session , '_timeLeft' ) . mockReturnValue ( 0 ) ; window . dispatchEvent ( event ) ; expect ( event . preventDefault ) . not . toHaveBeenCalled ( ) ; ctx . storeParties . setParties ( { 'random-123-sid' : [ ] } ) ; retVal = current . verifyAndConfirm ( docTerminal ) ; expect ( retVal ) . toBe ( false ) ; expect ( window . confirm ) . toHaveReturnedWith ( false ) ; jest . spyOn ( session , '_timeLeft' ) . mockReturnValue ( 5 ) ; window . dispatchEvent ( event ) ; expect ( event . preventDefault ) . toHaveBeenCalledTimes ( 1 ) ; } ) ;