<commit_before>const moment = require('moment')
const StoreUpdateModule = require('nativescript-store-update')
const appSettings = require('tns-core-modules/application-settings')
const dialogs = require('tns-core-modules/ui/dialogs')
const StoreUpdate = StoreUpdateModule.StoreUpdate
const UpdateTypesConstants = StoreUpdateModule.UpdateTypesConstants
const AlertTypesConstants = StoreUpdateModule.AlertTypesConstants

const config = {
  majorUpdateAlertType: AlertTypesConstants.FORCE,
  minorUpdateAlertType: AlertTypesConstants.OPTION,
  patchUpdateAlertType: AlertTypesConstants.NONE,
  revisionUpdateAlertType: AlertTypesConstants.OPTION,
  notifyNbDaysAfterRelease: 2,
  countryCode: 'ca',
}

describe('StoreUpdate ', () => {

  describe('init function', () => {
    beforeAll(() => {
      StoreUpdate.init(config)
    })

    it('should set instatiated to true', () => {
      expect(StoreUpdate.instatiated).toBe(true)
    })

    it('should set _majorUpdateAlertType to config', () => {
      expect(StoreUpdate._majorUpdateAlertType).toEqual(config.majorUpdateAlertType)
    })

    it('should set _minorUpdateAlertType to config', () => {
      expect(StoreUpdate._minorUpdateAlertType).toEqual(config.minorUpdateAlertType)
    })

    it('should set _patchUpdateAlertType to config', () => {
      expect(StoreUpdate._patchUpdateAlertType).toEqual(config.patchUpdateAlertType)
    })

    it('should set _revisionUpdateAlertType to config', () => {
      expect(StoreUpdate._revisionUpdateAlertType).toEqual(config.revisionUpdateAlertType)
    })

    it('should set _notifyNbDaysAfterRelease to config', () => {
      expect(StoreUpdate._notifyNbDaysAfterRelease).toEqual(config.notifyNbDaysAfterRelease)
    })

    it('should set _countryCode to config', () => {
      expect(StoreUpdate._countryCode).toEqual(config.countryCode)
    })

    it('should not be possible to init twice', () => {
      const newConf = Object.assign({}, config, {
        countryCode: 'fr'
      })
      StoreUpdate.init(newConf)
      expect(StoreUpdate._countryCode).toEqual(config.countryCode)
    })

  })

  describe('_showAlertForUpdate function', () => {
    beforeAll(() => {
      spyOn(dialogs, 'confirm').and.returnValue(Promise.resolve())
    })
    it('should display config majorUpdateAlertType confirm for major update', () => {
      const skippable = config.majorUpdateAlertType !== AlertTypesConstants.FORCE
      const expectedOptions = StoreUpdate._buildDialogOptions({ skippable })
      StoreUpdate._showAlertForUpdate('2.1.1.1')
      expect(dialogs.confirm).toHaveBeenCalledWith(expectedOptions)
    })
    it('should display config minorUpdateAlertType confirm for minor update', () => {
      const skippable = config.minorUpdateAlertType !== AlertTypesConstants.FORCE
      const expectedOptions = StoreUpdate._buildDialogOptions({ skippable })
      StoreUpdate._showAlertForUpdate('1.2.1.1')<commit_msg>Add _buildDialogOptions method to Store update tests<commit_after>const moment = require('moment')
const StoreUpdateModule = require('nativescript-store-update')
const appSettings = require('tns-core-modules/application-settings')
const dialogs = require('tns-core-modules/ui/dialogs')
const StoreUpdate = StoreUpdateModule.StoreUpdate
const LocalesHelper = StoreUpdateModule.LocalesHelper
const UpdateTypesConstants = StoreUpdateModule.UpdateTypesConstants
const AlertTypesConstants = StoreUpdateModule.AlertTypesConstants

const config = {
  majorUpdateAlertType: AlertTypesConstants.FORCE,
  minorUpdateAlertType: AlertTypesConstants.OPTION,
  patchUpdateAlertType: AlertTypesConstants.NONE,
  revisionUpdateAlertType: AlertTypesConstants.OPTION,
  notifyNbDaysAfterRelease: 2,
  countryCode: 'ca',
}

describe('StoreUpdate ', () => {

  describe('init function', () => {
    beforeAll(() => {
      StoreUpdate.init(config)
    })

    it('should set instatiated to true', () => {
      expect(StoreUpdate.instatiated).toBe(true)
    })

    it('should set _majorUpdateAlertType to config', () => {
      expect(StoreUpdate._majorUpdateAlertType).toEqual(config.majorUpdateAlertType)
    })

    it('should set _minorUpdateAlertType to config', () => {
      expect(StoreUpdate._minorUpdateAlertType).toEqual(config.minorUpdateAlertType)
    })

    it('should set _patchUpdateAlertType to config', () => {
      expect(StoreUpdate._patchUpdateAlertType).toEqual(config.patchUpdateAlertType)
    })

    it('should set _revisionUpdateAlertType to config', () => {
      expect(StoreUpdate._revisionUpdateAlertType).toEqual(config.revisionUpdateAlertType)
    })

    it('should set _notifyNbDaysAfterRelease to config', () => {
      expect(StoreUpdate._notifyNbDaysAfterRelease).toEqual(config.notifyNbDaysAfterRelease)
    })

    it('should set _countryCode to config', () => {
      expect(StoreUpdate._countryCode).toEqual(config.countryCode)
    })

    it('should not be possible to init twice', () => {
      const newConf = Object.assign({}, config, {
        countryCode: 'fr'
      })
      StoreUpdate.init(newConf)
      expect(StoreUpdate._countryCode).toEqual(config.countryCode)
    })

  })

  describe('_buildDialogOptions function', () => {
    const defaultOptions = options = {
      message: LocalesHelper.translate('ALERT_MESSAGE'),
      neutralButtonText: null,
      okButtonText: LocalesHelper.translate('ALERT_UPDATE_BUTTON'),
      title: LocalesHelper.translate('ALERT_TITLE'),
    }
    const skippableOptions = Object.assign({}, defaultOptions, {
      neutralButtonText: LocalesHelper.translate('ALERT_SKIP_BUTTON'),
    })
    it('should return options with neutralButtonText by default', () => {
      expect(StoreUpdate._buildDialogOptions()).toEqual(skippableOptions)
    })
    it('should return options with neutralButtonText if skippable is true', () => {
      expect(StoreUpdate._buildDialogOptions({ skippable: true })).toEqual(skippableOptions)
    })
    it('should return options without neutralButtonText if skippable is false', () => {
      expect(StoreUpdate._buildDialogOptions({ skippable: false })).toEqual(defaultOptions)
    })
  })

  describe('_showAlertForUpdate function', () => {
    beforeAll(() => {
      spyOn(dialogs, 'confirm').and.returnValue(Promise.resolve())
    })
    it('should display config majorUpdateAlertType confirm for major update', () => {
      const skippable = config.majorUpdateAlertType !== AlertTypesConstants.FORCE
      const expectedOptions = StoreUpdate._buildDialogOptions({ skippable })
      StoreUpdate._showAlertForUpdate('2.1.1.1')
      expect(dialogs.confirm).toHaveBeenCalledWith(expectedOptions)
    })
    it('should display config minorUpdateAlertType confirm for minor update', () => {
      const skippable = config.minorUpdateAlertType !== AlertTypesConstants.FORCE
      const expectedOptions = StoreUpdate._buildDialogOptions({ skippable })
      StoreUpdate._showAlertForUpdate('1.2.1.1')
{'fraction_non_alphanumeric': 0.11686625306593565, 'fraction_numerical': 0.0025970278459096813, 'mean_word_length': 4.572347266881029, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1767633', 'n_tokens_mistral': 1925, 'n_tokens_neox': 1895, 'n_words': 335}