import { IMock , It , Mock , Times } from 'typemoq' ; import { Desktop } from '../../core/desktop' ; import { GitHubApi } from '../../core/github-api' ; import { Logger } from '../../core/logger' ; import { ProductInformation } from '../../core/product-information' ; import { Settings } from '../../core/settings' ; import { UpdateService } from './update.service' ; describe ( 'UpdateService' , ( ) => { let settingsMock ; let loggerMock ; let gitHubMock ; let desktopMock ; let service ; beforeEach ( ( ) => { settingsMock = Mock . ofType < Settings > ( ) ; loggerMock = Mock . ofType < Logger > ( ) ; gitHubMock = Mock . ofType < GitHubApi > ( ) ; desktopMock = Mock . ofType < Desktop > ( ) ; service = new UpdateService ( settingsMock . object , loggerMock . object , gitHubMock . object , desktopMock . object ) ; } ) ; describe ( 'constructor' , ( ) => { it ( 'should create' , ( ) => { expect ( service ) . toBeDefined ( ) ; } ) ; it ( 'should define isUpdateAvailable as false' , ( ) => { expect ( service . isUpdateAvailable ) . toBeFalsy ( ) ; } ) ; it ( 'should define latestRelease as empty' , ( ) => { expect ( service . latestRelease ) . toEqual ( '' ) ; } ) ; } ) ; describe ( 'checkForUpdatesAsync' , ( ) => { it ( 'should not check for updates if not requested' , async ( ) => { settingsMock . setup ( ( x ) => x . checkForUpdates ) . returns ( ( ) => false ) ; service = new UpdateService ( settingsMock . object , loggerMock . object , gitHubMock . object , desktopMock . object ) ; await service . checkForUpdatesAsync ( ) ; gitHubMock . verify ( ( x ) => x . getLatestReleaseAsync ( 'digimezzo' , 'knowte' , It . isAny ( ) ) , Times . never ( ) ) ; } ) ; it ( 'should check for updates excluding pre-releases if requested' , async ( ) => { settingsMock . setup ( ( x ) => x . checkForUpdates ) . returns ( ( ) => true ) ; service = new UpdateService ( settingsMock . object , loggerMock . object , gitHubMock . object , desktopMock . object ) ; await service . checkForUpdatesAsync ( ) ; gitHubMock . verify ( ( x ) => x . getLatestReleaseAsync ( 'digimezzo' , 'knowte' , false ) , Times . exactly ( 1 ) ) ; } ) ; it ( 'should indicate that an update is available if the latest release is newer than the current release' , async ( ) => { gitHubMock . setup ( ( x ) => x . getLatestReleaseAsync ( 'digimezzo' , 'knowte' , false ) ) . returns ( async ( ) => '1000.0.0.0' ) ; service = new UpdateService ( settingsMock . object , loggerMock . object , gitHubMock . object , desktopMock . object ) ; await service . checkForUpdatesAsync ( ) ; expect ( service . isUpdateAvailable ) . toBeTruthy ( ) ; expect ( service . latestRelease ) . toEqual ( '1000.0.0.0' ) ; } ) ; it ( 'should not indicate that an update is available if the latest release is equal to the current release' , async ( ) => { gitHubMock . setup ( ( x ) => x . getLatestReleaseAsync ( 'digimezzo' , 'knowte' , false ) ) . returns ( async ( ) => ProductInformation . applicationVersion ) ; service = new UpdateService ( settingsMock . object , loggerMock . object , gitHubMock . object , desktopMock . object ) ; await service . checkForUpdatesAsync ( ) ; expect ( service . isUpdateAvailable ) . toBeFalsy ( ) ; expect ( service . latestRelease ) . toEqual ( '' ) ; } ) ; it ( 'should not indicate that an update is available if the latest release is older than the current release' , async ( ) => { gitHubMock . setup ( ( x ) => x . getLatestReleaseAsync ( 'digimezzo' , 'knowte' , false ) ) . returns ( async ( ) => '1.0.0' ) ; service = new UpdateService ( settingsMock . object , loggerMock . object , gitHubMock . object , desktopMock . object ) ; await service . checkForUpdatesAsync ( ) ; expect ( service . isUpdateAvailable ) . toBeFalsy ( ) ; expect ( service . latestRelease ) . toEqual ( '' ) ; } ) ; } ) ; describe ( 'downloadLatestRelease' , ( ) => { it ( 'should download the latest release' , ( ) => { service . downloadLatestRelease ( ) ; desktopMock . verify ( ( x ) => x . openLink ( It . isAny ( ) ) , Times . exactly ( 1 ) ) ; } ) ; } ) ; } ) ;