How to use targetService method in ng-mocks

Best JavaScript code snippet using ng-mocks

AuraLocalizationServiceTest.js

Source:AuraLocalizationServiceTest.js Github

copy

Full Screen

1/*2 * Copyright (C) 2013 salesforce.com, inc.3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16Function.RegisterNamespace("Test.Aura");17[Fixture]18Test.Aura.AuraLocalizationServiceTest = function() {19 var Aura = {20 Services: {21 }22 };23 // Mock the exp() function defined in Aura.js, this is originally used for exposing members using a export.js file24 Mocks.GetMocks(Object.Global(), {25 "Aura": Aura,26 "AuraLocalizationService":function(){}27 })(function() {28 [Import("aura-impl/src/main/resources/aura/AuraLocalizationService.js")]29 });30 var targetService = new Aura.Services.AuraLocalizationService();31 var targetDate = "07/10/2013";32 var targetDateFormat = "DD-MM-YYYY";33 var targetDateTime = "07/10/2013 12:00:00";34 var targetDateTimeFormat = "DD-MM-YYYY hh:mm:ss";35 var targetTime = "12:00:00";36 var targetTimeFormat = "hh:mm:ss";37 var targetLocale = "en";38 var targetTimezone = "PST";39 var targetNumber = 101;40 var targetPercent = '10%';41 var targetCurrency = '$100';42 var targetNumberFormat = "nFormat";43 var targetPercentFormat = "pFormat";44 var targetCurrencyFormat = "cFormat";45 var mockUtil = Mocks.GetMock(Object.Global(), "$A", {46 assert: function () {},47 auraError: function() {},48 get:function(value){49 if(value === "$Locale.dateFormat") return targetDateFormat;50 if(value === "$Locale.datetimeFormat") return targetDateTimeFormat;51 if(value === "$Locale.timeFormat") return targetTimeFormat;52 if(value === "$Locale.timezone") return targetTimezone;53 if(value === "$Locale.numberFormat") return targetNumberFormat;54 if(value === "$Locale.percentFormat") return targetPercentFormat;55 if(value === "$Locale.currencyFormat") return targetCurrencyFormat;56 },57 clientService: {58 loadClientLibrary: function (name, callback) {59 callback();60 }61 },62 lockerService: {63 instanceOf: function(value, type) {64 return value instanceof type;65 }66 },67 logger: {68 reportError: function(){}69 },70 warning: function(){}71 });72 var mockDate = {73 isValid:function(){74 return true;75 },76 toString:function(){77 return targetDate;78 }79 };80 var mockDateTime = {81 isValid:function(){82 return true;83 },84 toString:function(){85 return targetDateTime;86 },87 toDate:function(){88 return targetDateTime;89 }90 };91 [Fixture]92 function displayDuration(){93 var targetNoSuffix = "noSuffix";94 var targetDuration={95 humanize:function(noSuffix){96 if(noSuffix === targetNoSuffix)return true;97 },98 asDays:function(){99 return "365";100 },101 asHours:function(){102 return "24";103 },104 asMilliseconds:function(){105 return "3600000";106 },107 asMinutes:function(){108 return "60";109 },110 asMonths:function(){111 return "12";112 },113 asSeconds:function(){114 return "3600";115 },116 asYears:function(){117 return "2013";118 },119 days:function(){120 return "365";121 },122 hours:function(){123 return "24";124 },125 milliseconds:function(){126 return "3600000";127 },128 minutes:function(){129 return "60";130 },131 months:function(){132 return "12";133 },134 seconds:function(){135 return "3600";136 },137 years:function(){138 return "2013";139 }140 };141 [Fact]142 function displayDuration(){143 // Arrange144 var expected = true;145 var actual;146 // Act147 actual = targetService.displayDuration(targetDuration, targetNoSuffix);148 // Assert149 Assert.Equal(expected, actual);150 }151 [Fact]152 function displayDurationInDays(){153 // Arrange154 var expected = "365";155 var actual;156 // Act157 actual = targetService.displayDurationInDays(targetDuration);158 // Assert159 Assert.Equal(expected, actual);160 }161 [Fact]162 function displayDurationInHours(){163 // Arrange164 var expected = "24";165 var actual;166 // Act167 actual = targetService.displayDurationInHours(targetDuration);168 // Assert169 Assert.Equal(expected, actual);170 }171 [Fact]172 function displayDurationInMilliseconds(){173 // Arrange174 var expected = "3600000";175 var actual;176 // Act177 actual = targetService.displayDurationInMilliseconds(targetDuration);178 // Assert179 Assert.Equal(expected, actual);180 }181 [Fact]182 function displayDurationInMinutes(){183 // Arrange184 var expected = "60";185 var actual;186 // Act187 actual = targetService.displayDurationInMinutes(targetDuration);188 // Assert189 Assert.Equal(expected, actual);190 }191 [Fact]192 function displayDurationInMonths(){193 // Arrange194 var expected = "12";195 var actual;196 // Act197 actual = targetService.displayDurationInMonths(targetDuration);198 // Assert199 Assert.Equal(expected, actual);200 }201 [Fact]202 function displayDurationInSeconds(){203 // Arrange204 var expected = "3600";205 var actual;206 // Act207 actual = targetService.displayDurationInSeconds(targetDuration);208 // Assert209 Assert.Equal(expected, actual);210 }211 [Fact]212 function displayDurationInYears(){213 // Arrange214 var expected = "2013";215 var actual;216 // Act217 actual = targetService.displayDurationInYears(targetDuration);218 // Assert219 Assert.Equal(expected, actual);220 }221 [Fact]222 function getDaysInDuration(){223 // Arrange224 var expected = "365";225 var actual;226 // Act227 actual = targetService.getDaysInDuration(targetDuration);228 // Assert229 Assert.Equal(expected, actual);230 }231 [Fact]232 function getHoursInDuration(){233 // Arrange234 var expected = "24";235 var actual;236 // Act237 actual = targetService.getHoursInDuration(targetDuration);238 // Assert239 Assert.Equal(expected, actual);240 }241 [Fact]242 function getMillisecondsInDuration(){243 // Arrange244 var expected = "3600000";245 var actual;246 // Act247 actual = targetService.getMillisecondsInDuration(targetDuration);248 // Assert249 Assert.Equal(expected, actual);250 }251 [Fact]252 function getMinutesInDuration(){253 // Arrange254 var expected = "60";255 var actual;256 // Act257 actual = targetService.getMinutesInDuration(targetDuration);258 // Assert259 Assert.Equal(expected, actual);260 }261 [Fact]262 function getMonthsInDuration(){263 // Arrange264 var expected = "12";265 var actual;266 // Act267 actual = targetService.getMonthsInDuration(targetDuration);268 // Assert269 Assert.Equal(expected, actual);270 }271 [Fact]272 function getSecondsInDuration(){273 // Arrange274 var expected = "3600";275 var actual;276 // Act277 actual = targetService.getSecondsInDuration(targetDuration);278 // Assert279 Assert.Equal(expected, actual);280 }281 [Fact]282 function getYearsInDuration(){283 // Arrange284 var expected = "2013";285 var actual;286 // Act287 actual = targetService.getYearsInDuration(targetDuration);288 // Assert289 Assert.Equal(expected, actual);290 }291 }292 [Fixture]293 function duration(){294 var targetNum = "Num";295 var targetUnit = "Unit";296 var mockMoment = {297 duration:function(num, unit){298 if(unit){299 if(num === targetNum && unit === targetUnit)return "With Unit";300 }301 else{302 if(num === targetNum)return "Without Unit";303 }304 }305 };306 var createTargetService = function() {307 var targetService = new Aura.Services.AuraLocalizationService();308 targetService.moment = mockMoment;309 return targetService;310 }311 [Fact]312 function durationWithoutUnit(){313 // Arrange314 var expected = "Without Unit";315 var targetService = createTargetService();316 // Act317 var actual = targetService.duration(targetNum, undefined);318 // Assert319 Assert.Equal(expected, actual);320 }321 [Fact]322 function durationWithUnit(){323 // Arrange324 var expected = "With Unit";325 var targetService = createTargetService();326 // Act327 var actual = targetService.duration(targetNum, targetUnit);328 // Assert329 Assert.Equal(expected, actual);330 }331 }332 [Fixture]333 function EndOf() {334 var targetDate = "date";335 var targetUnit = "Unit";336 var mockMomentConstr = function(date) {337 if (date === targetDate) {338 return {339 endOf: function(unit) {340 if (unit === targetUnit) {341 return {342 toDate: function() {343 return "endOf";344 }345 };346 }347 }348 }349 }350 };351 var createTargetService = function() {352 var targetService = new Aura.Services.AuraLocalizationService();353 targetService.moment = mockMomentConstr;354 return targetService;355 }356 [Fact]357 function CallsMomentEndOf(){358 // Arrange359 var expected = "endOf";360 var targetService = createTargetService();361 // Act362 var actual = targetService.endOf(targetDate, targetUnit);363 // Assert364 Assert.Equal(expected, actual);365 }366 }367 [Fixture]368 function StartOf() {369 var targetDate = "date";370 var targetUnit = "Unit";371 var mockMomentConstr = function(date) {372 if (date === targetDate) {373 return {374 startOf: function(unit) {375 if (unit === targetUnit) {376 return {377 toDate: function() {378 return "startOf";379 }380 };381 }382 }383 }384 }385 };386 var createTargetService = function() {387 var targetService = new Aura.Services.AuraLocalizationService();388 targetService.moment = mockMomentConstr;389 return targetService;390 }391 [Fact]392 function CallsMomentStartOf(){393 // Arrange394 var expected = "startOf";395 var targetService = createTargetService();396 // Act397 var actual = targetService.startOf(targetDate, targetUnit);398 // Assert399 Assert.Equal(expected, actual);400 }401 }402 [Fixture]403 function formatDate(){404 [Fact]405 function CallsDisplayDateTimeWithMomentDateForValidDate(){406 // Arrange407 var actual;408 var targetService = new Aura.Services.AuraLocalizationService();409 targetService.displayDateTime = function (date, format, locale) {410 actual = date;411 }412 var momentDate = {413 isValid: function() {414 return true;415 }416 };417 targetService.moment = function() {418 return momentDate;419 };420 var mockUtil = Mocks.GetMock(Object.Global(), "$A", {421 util: {422 isUndefined: function(value) { return value === undefined; }423 },424 get: function(value) {}425 });426 // Act427 mockUtil(function(){428 targetService.formatDate("date", "format");429 });430 // Assert431 Assert.Equal(momentDate, actual);432 }433 [Fact]434 function UsesDateFormatInLocaleProviderAsDefaultFormat(){435 // Arrange436 var actual;437 var targetService = new Aura.Services.AuraLocalizationService();438 targetService.displayDateTime = function (date, format, locale) {439 actual = format;440 }441 var momentDate = {442 isValid: function() {443 return true;444 }445 };446 targetService.moment = function() {447 return momentDate;448 };449 var expected = "format";450 var mockUtil = Mocks.GetMock(Object.Global(), "$A", {451 util: {452 isUndefined: function(value) { return value === undefined; }453 },454 get: function(value){455 if(value === "$Locale.dateFormat") return expected;456 }457 });458 // Act459 mockUtil(function(){460 targetService.formatDate("date");461 });462 // Assert463 Assert.Equal(expected, actual);464 }465 [Fact]466 function UsesLocaleInLocaleProvider(){467 // Arrange468 var actual;469 var targetService = new Aura.Services.AuraLocalizationService();470 targetService.displayDateTime = function (date, format, locale) {471 actual = locale;472 }473 var momentDate = {474 isValid: function() {475 return true;476 }477 };478 targetService.moment = function() {479 return momentDate;480 };481 var expected = "locale";482 var mockUtil = Mocks.GetMock(Object.Global(), "$A", {483 util: {484 isUndefined: function(value) { return value === undefined; }485 },486 get: function(value){487 if(value === "$Locale.langLocale") return expected;488 }489 });490 // Act491 mockUtil(function(){492 targetService.formatDate("date", "format");493 });494 // Assert495 Assert.Equal(expected, actual);496 }497 }498 [Fixture]499 function formatDateUTC(){500 [Fact]501 function CallsDisplayDateTimeWithMomentDateForValidDate(){502 // Arrange503 var actual;504 var targetService = new Aura.Services.AuraLocalizationService();505 targetService.displayDateTime = function (date, format, locale) {506 actual = date;507 }508 var momentDate = {509 isValid: function() {510 return true;511 }512 };513 targetService.moment = {514 utc: function() {515 return momentDate;516 }517 };518 var mockUtil = Mocks.GetMock(Object.Global(), "$A", {519 util: {520 isUndefined: function(value) { return value === undefined; }521 },522 get: function(value) {}523 });524 // Act525 mockUtil(function(){526 targetService.formatDateUTC("date", "format");527 });528 // Assert529 Assert.Equal(momentDate, actual);530 }531 [Fact]532 function UsesDateFormatInLocaleProviderAsDefaultFormat(){533 // Arrange534 var actual;535 var targetService = new Aura.Services.AuraLocalizationService();536 targetService.displayDateTime = function (date, format, locale) {537 actual = format;538 }539 var momentDate = {540 isValid: function() {541 return true;542 }543 };544 targetService.moment = {545 utc: function() {546 return momentDate;547 }548 };549 var expected = "format";550 var mockUtil = Mocks.GetMock(Object.Global(), "$A", {551 util: {552 isUndefined: function(value) { return value === undefined; }553 },554 get: function(value){555 if(value === "$Locale.dateFormat") return expected;556 }557 });558 // Act559 mockUtil(function(){560 targetService.formatDateUTC("date");561 });562 // Assert563 Assert.Equal(expected, actual);564 }565 [Fact]566 function UsesLocaleInLocaleProvider(){567 // Arrange568 var actual;569 var targetService = new Aura.Services.AuraLocalizationService();570 targetService.displayDateTime = function (date, format, locale) {571 actual = locale;572 }573 var momentDate = {574 isValid: function() {575 return true;576 }577 };578 targetService.moment = {579 utc: function() {580 return momentDate;581 }582 };583 var expected = "locale";584 var mockUtil = Mocks.GetMock(Object.Global(), "$A", {585 util: {586 isUndefined: function(value) { return value === undefined; }587 },588 get: function(value){589 if(value === "$Locale.langLocale") return expected;590 }591 });592 // Act593 mockUtil(function(){594 targetService.formatDateUTC("date", "format");595 });596 // Assert597 Assert.Equal(expected, actual);598 }599 }600 [Fixture]601 function formatDateTime(){602 [Fact]603 function CallsDisplayDateTimeWithMomentDateForValidDate(){604 // Arrange605 var actual;606 var targetService = new Aura.Services.AuraLocalizationService();607 targetService.displayDateTime = function (date, format, locale) {608 actual = date;609 }610 var momentDate = {611 isValid: function() {612 return true;613 }614 };615 targetService.moment = function() {616 return momentDate;617 };618 var mockUtil = Mocks.GetMock(Object.Global(), "$A", {619 util: {620 isUndefined: function(value) { return value === undefined; }621 },622 get: function(value) {}623 });624 // Act625 mockUtil(function(){626 targetService.formatDateTime("date", "format");627 });628 // Assert629 Assert.Equal(momentDate, actual);630 }631 [Fact]632 function UsesDateTimeFormatInLocaleProviderAsDefaultFormat(){633 // Arrange634 var actual;635 var targetService = new Aura.Services.AuraLocalizationService();636 targetService.displayDateTime = function (date, format, locale) {637 actual = format;638 }639 var momentDate = {640 isValid: function() {641 return true;642 }643 };644 targetService.moment = function() {645 return momentDate;646 };647 var expected = "format";648 var mockUtil = Mocks.GetMock(Object.Global(), "$A", {649 util: {650 isUndefined: function(value) { return value === undefined; }651 },652 get: function(value){653 if(value === "$Locale.datetimeFormat") return expected;654 }655 });656 // Act657 mockUtil(function(){658 targetService.formatDateTime("date");659 });660 // Assert661 Assert.Equal(expected, actual);662 }663 [Fact]664 function UsesLocaleInLocaleProvider(){665 // Arrange666 var actual;667 var targetService = new Aura.Services.AuraLocalizationService();668 targetService.displayDateTime = function (date, format, locale) {669 actual = locale;670 }671 var momentDate = {672 isValid: function() {673 return true;674 }675 };676 targetService.moment = function() {677 return momentDate;678 };679 var expected = "locale";680 var mockUtil = Mocks.GetMock(Object.Global(), "$A", {681 util: {682 isUndefined: function(value) { return value === undefined; }683 },684 get: function(value){685 if(value === "$Locale.langLocale") return expected;686 }687 });688 // Act689 mockUtil(function(){690 targetService.formatDateTime("date", "format");691 });692 // Assert693 Assert.Equal(expected, actual);694 }695 }696 [Fixture]697 function formatDateTimeUTC(){698 [Fact]699 function CallsDisplayDateTimeWithMomentDateForValidDate(){700 // Arrange701 var actual;702 var targetService = new Aura.Services.AuraLocalizationService();703 targetService.displayDateTime = function (date, format, locale) {704 actual = date;705 }706 var momentDate = {707 isValid: function() {708 return true;709 }710 };711 targetService.moment = {712 utc: function() {713 return momentDate;714 }715 };716 var mockUtil = Mocks.GetMock(Object.Global(), "$A", {717 util: {718 isUndefined: function(value) { return value === undefined; }719 },720 get: function(value) {}721 });722 // Act723 mockUtil(function(){724 targetService.formatDateTimeUTC("date", "format");725 });726 // Assert727 Assert.Equal(momentDate, actual);728 }729 [Fact]730 function UsesDateTimeFormatInLocaleProviderAsDefaultFormat(){731 // Arrange732 var actual;733 var targetService = new Aura.Services.AuraLocalizationService();734 targetService.displayDateTime = function (date, format, locale) {735 actual = format;736 }737 var momentDate = {738 isValid: function() {739 return true;740 }741 };742 targetService.moment = {743 utc: function() {744 return momentDate;745 }746 };747 var expected = "format";748 var mockUtil = Mocks.GetMock(Object.Global(), "$A", {749 util: {750 isUndefined: function(value) { return value === undefined; }751 },752 get: function(value){753 if(value === "$Locale.datetimeFormat") return expected;754 }755 });756 // Act757 mockUtil(function(){758 targetService.formatDateTimeUTC("date");759 });760 // Assert761 Assert.Equal(expected, actual);762 }763 [Fact]764 function UsesLocaleInLocaleProvider(){765 // Arrange766 var actual;767 var targetService = new Aura.Services.AuraLocalizationService();768 targetService.displayDateTime = function (date, format, locale) {769 actual = locale;770 }771 var momentDate = {772 isValid: function() {773 return true;774 }775 };776 targetService.moment = {777 utc: function() {778 return momentDate;779 }780 };781 var expected = "locale";782 var mockUtil = Mocks.GetMock(Object.Global(), "$A", {783 util: {784 isUndefined: function(value) { return value === undefined; }785 },786 get: function(value){787 if(value === "$Locale.langLocale") return expected;788 }789 });790 // Act791 mockUtil(function(){792 targetService.formatDateTimeUTC("date", "format");793 });794 // Assert795 Assert.Equal(expected, actual);796 }797 }798 [Fixture]799 function formatTime() {800 [Fact]801 function CallsDisplayDateTimeWithMomentDateForValidDate(){802 // Arrange803 var actual;804 var targetService = new Aura.Services.AuraLocalizationService();805 targetService.displayDateTime = function (date, format, locale) {806 actual = date;807 }808 var momentDate = {809 isValid: function() {810 return true;811 }812 };813 targetService.moment = function() {814 return momentDate;815 };816 var mockUtil = Mocks.GetMock(Object.Global(), "$A", {817 util: {818 isUndefined: function(value) { return value === undefined; }819 },820 get: function(value) {}821 });822 // Act823 mockUtil(function(){824 targetService.formatTime("date", "format");825 });826 // Assert827 Assert.Equal(momentDate, actual);828 }829 [Fact]830 function UsesDateFormatInLocaleProviderAsDefaultFormat(){831 // Arrange832 var actual;833 var targetService = new Aura.Services.AuraLocalizationService();834 targetService.displayDateTime = function (date, format, locale) {835 actual = format;836 }837 var momentDate = {838 isValid: function() {839 return true;840 }841 };842 targetService.moment = function() {843 return momentDate;844 };845 var expected = "format";846 var mockUtil = Mocks.GetMock(Object.Global(), "$A", {847 util: {848 isUndefined: function(value) { return value === undefined; }849 },850 get: function(value){851 if(value === "$Locale.timeFormat") return expected;852 }853 });854 // Act855 mockUtil(function(){856 targetService.formatTime("date");857 });858 // Assert859 Assert.Equal(expected, actual);860 }861 [Fact]862 function UsesLocaleInLocaleProvider(){863 // Arrange864 var actual;865 var targetService = new Aura.Services.AuraLocalizationService();866 targetService.displayDateTime = function (date, format, locale) {867 actual = locale;868 }869 var momentDate = {870 isValid: function() {871 return true;872 }873 };874 targetService.moment = function() {875 return momentDate;876 };877 var expected = "locale";878 var mockUtil = Mocks.GetMock(Object.Global(), "$A", {879 util: {880 isUndefined: function(value) { return value === undefined; }881 },882 get: function(value){883 if(value === "$Locale.langLocale") return expected;884 }885 });886 // Act887 mockUtil(function(){888 targetService.formatTime("date", "format");889 });890 // Assert891 Assert.Equal(expected, actual);892 }893 }894 [Fixture]895 function formatTimeUTC(){896 [Fact]897 function CallsDisplayDateTimeWithMomentDateForValidDate(){898 // Arrange899 var actual;900 var targetService = new Aura.Services.AuraLocalizationService();901 targetService.displayDateTime = function (date, format, locale) {902 actual = date;903 }904 var momentDate = {905 isValid: function() {906 return true;907 }908 };909 targetService.moment = {910 utc: function() {911 return momentDate;912 }913 };914 var mockUtil = Mocks.GetMock(Object.Global(), "$A", {915 util: {916 isUndefined: function(value) { return value === undefined; }917 },918 get: function(value) {}919 });920 // Act921 mockUtil(function(){922 targetService.formatTimeUTC("date", "format");923 });924 // Assert925 Assert.Equal(momentDate, actual);926 }927 [Fact]928 function UsesTimeFormatInLocaleProviderAsDefaultFormat(){929 // Arrange930 var actual;931 var targetService = new Aura.Services.AuraLocalizationService();932 targetService.displayDateTime = function (date, format, locale) {933 actual = format;934 }935 var momentDate = {936 isValid: function() {937 return true;938 }939 };940 targetService.moment = {941 utc: function() {942 return momentDate;943 }944 };945 var expected = "format";946 var mockUtil = Mocks.GetMock(Object.Global(), "$A", {947 util: {948 isUndefined: function(value) { return value === undefined; }949 },950 get: function(value){951 if(value === "$Locale.timeFormat") return expected;952 }953 });954 // Act955 mockUtil(function(){956 targetService.formatTimeUTC("date");957 });958 // Assert959 Assert.Equal(expected, actual);960 }961 [Fact]962 function UsesLocaleInLocaleProvider(){963 // Arrange964 var actual;965 var targetService = new Aura.Services.AuraLocalizationService();966 targetService.displayDateTime = function (date, format, locale) {967 actual = locale;968 }969 var momentDate = {970 isValid: function() {971 return true;972 }973 };974 targetService.moment = {975 utc: function() {976 return momentDate;977 }978 };979 var expected = "locale";980 var mockUtil = Mocks.GetMock(Object.Global(), "$A", {981 util: {982 isUndefined: function(value) { return value === undefined; }983 },984 get: function(value){985 if(value === "$Locale.langLocale") return expected;986 }987 });988 // Act989 mockUtil(function(){990 targetService.formatTimeUTC("date", "format");991 });992 // Assert993 Assert.Equal(expected, actual);994 }995 }996 [Fixture]997 function DateComparisons(){998 var targetDate1 = "date1";999 var targetDate2 = "date2";1000 var targetUnit = "unit";1001 var mockDuration = {1002 isAfter:function(date2, unit){1003 if(date2 === targetDate2 && unit === targetUnit) return "isAfter";1004 },1005 isBefore:function(date2, unit){1006 if(date2 === targetDate2 && unit === targetUnit) return "isBefore";1007 },1008 isSame:function(date2, unit){1009 if(date2 === targetDate2 && unit === targetUnit) return "isSame";1010 }1011 };1012 var createTargetService = function() {1013 var targetService = new Aura.Services.AuraLocalizationService();1014 targetService.moment = function(date) {1015 if (date === targetDate1) return mockDuration;1016 }1017 return targetService;1018 }1019 [Fact]1020 function isAfter(){1021 // Arrange1022 var expected = "isAfter";1023 var targetService = createTargetService();1024 //Act1025 var actual = targetService.isAfter(targetDate1, targetDate2, targetUnit);1026 // Assert1027 Assert.Equal(expected, actual);1028 }1029 [Fact]1030 function isBefore(){1031 // Arrange1032 var expected = "isBefore";1033 var targetService = createTargetService();1034 //Act1035 var actual = targetService.isBefore(targetDate1, targetDate2, targetUnit);1036 // Assert1037 Assert.Equal(expected, actual);1038 }1039 [Fact]1040 function isSame(){1041 // Arrange1042 var expected = "isSame";1043 var targetService = createTargetService();1044 //Act1045 var actual = targetService.isSame(targetDate1, targetDate2, targetUnit);1046 // Assert1047 Assert.Equal(expected, actual);1048 }1049 }1050 [Fixture]1051 function parseDateTime() {1052 [Fact]1053 function ReturnsNullForEmptyDateTimeString() {1054 // Arrange1055 var targetService = new Aura.Services.AuraLocalizationService();1056 // Act1057 var actual = targetService.parseDateTime("", "format");1058 // Assert1059 Assert.Null(actual);1060 }1061 [Fact]1062 function ReturnsNullIfMomentDateIsInvalid() {1063 // Arrange1064 var actual;1065 var targetService = new Aura.Services.AuraLocalizationService();1066 var momentDate = {1067 isValid: function() {1068 return false;1069 }1070 };1071 targetService.moment = function() {1072 return momentDate;1073 };1074 var mockUtil = Mocks.GetMock(Object.Global(), "$A", {1075 util: {1076 isBoolean: function(value) { return typeof value === "boolean" },1077 isUndefined: function(value) { return value === undefined; }1078 },1079 get: function(value){}1080 });1081 // Act1082 mockUtil(function(){1083 actual = targetService.parseDateTime("date", "format");1084 });1085 // Assert1086 Assert.Null(actual);1087 }1088 [Fact]1089 function CallsToDateIfMomentDateIsValid() {1090 // Arrange1091 var actual;1092 var targetService = new Aura.Services.AuraLocalizationService();1093 var momentDate = {1094 isValid: function() {1095 return true;1096 },1097 toDate: Stubs.GetMethod()1098 };1099 targetService.moment = function() {1100 return momentDate;1101 };1102 var mockUtil = Mocks.GetMock(Object.Global(), "$A", {1103 util: {1104 isBoolean: function(value) { return typeof value === "boolean" },1105 isUndefined: function(value) { return value === undefined; }1106 },1107 get: function(value){}1108 });1109 // Act1110 mockUtil(function(){1111 targetService.parseDateTime("date", "format");1112 });1113 // Assert1114 Assert.Equal(1, momentDate.toDate.Calls.length);1115 }1116 [Fact]1117 function UsesLocaleInLocaleProviderIfThirdParamIsBoolean() {1118 // Arrange1119 var actual;1120 var targetService = new Aura.Services.AuraLocalizationService();1121 targetService.getAvailableMomentLocale = function(locale) {1122 actual = locale;1123 }1124 var momentDate = {1125 isValid: function() {1126 return true;1127 },1128 toDate: function() {}1129 };1130 targetService.moment = function() {1131 return momentDate;1132 };1133 var expected = "locale";1134 var mockUtil = Mocks.GetMock(Object.Global(), "$A", {1135 util: {1136 isBoolean: function(value) { return typeof value === "boolean" },1137 isUndefined: function(value) { return value === undefined; }1138 },1139 get: function(value){1140 if(value === "$Locale.langLocale") return expected;1141 }1142 });1143 // Act1144 mockUtil(function(){1145 targetService.parseDateTime("date", "format", true);1146 });1147 // Assert1148 Assert.Equal(expected, actual);1149 }1150 [Fact]1151 function UsesLocaleInLocaleProviderIfThirdParamIsUndefined() {1152 // Arrange1153 var actual;1154 var targetService = new Aura.Services.AuraLocalizationService();1155 targetService.getAvailableMomentLocale = function(locale) {1156 actual = locale;1157 }1158 var momentDate = {1159 isValid: function() {1160 return true;1161 },1162 toDate: function() {}1163 };1164 targetService.moment = function() {1165 return momentDate;1166 };1167 var expected = "locale";1168 var mockUtil = Mocks.GetMock(Object.Global(), "$A", {1169 util: {1170 isBoolean: function(value) { return typeof value === "boolean" },1171 isUndefined: function(value) { return value === undefined; }1172 },1173 get: function(value){1174 if(value === "$Locale.langLocale") return expected;1175 }1176 });1177 // Act1178 mockUtil(function(){1179 targetService.parseDateTime("date", "format");1180 });1181 // Assert1182 Assert.Equal(expected, actual);1183 }1184 }1185 [Fixture]1186 function parseDateTimeUTC(){1187 [Fact]1188 function ReturnsNullForEmptyDateTimeString() {1189 // Arrange1190 var targetService = new Aura.Services.AuraLocalizationService();1191 // Act1192 var actual = targetService.parseDateTime("", "format");1193 // Assert1194 Assert.Null(actual);1195 }1196 [Fact]1197 function ReturnsNullIfMomentDateIsInvalid() {1198 // Arrange1199 var actual;1200 var targetService = new Aura.Services.AuraLocalizationService();1201 var momentDate = {1202 isValid: function() {1203 return false;1204 }1205 };1206 targetService.moment = {1207 utc: function() {1208 return momentDate;1209 }1210 };1211 var mockUtil = Mocks.GetMock(Object.Global(), "$A", {1212 util: {1213 isBoolean: function(value) { return typeof value === "boolean" },1214 isUndefined: function(value) { return value === undefined; }1215 },1216 get: function(value) {}1217 });1218 // Act1219 mockUtil(function(){1220 actual = targetService.parseDateTimeUTC("date", "format");1221 });1222 // Assert1223 Assert.Null(actual);1224 }1225 [Fact]1226 function CallsToDateIfMomentDateIsValid() {1227 // Arrange1228 var actual;1229 var targetService = new Aura.Services.AuraLocalizationService();1230 var momentDate = {1231 isValid: function() {1232 return true;1233 },1234 toDate: Stubs.GetMethod()1235 };1236 targetService.moment = {1237 utc: function() {1238 return momentDate;1239 }1240 };1241 var mockUtil = Mocks.GetMock(Object.Global(), "$A", {1242 util: {1243 isBoolean: function(value) { return typeof value === "boolean" },1244 isUndefined: function(value) { return value === undefined; }1245 },1246 get: function(value) {}1247 });1248 // Act1249 mockUtil(function(){1250 targetService.parseDateTimeUTC("date", "format");1251 });1252 // Assert1253 Assert.Equal(1, momentDate.toDate.Calls.length);1254 }1255 [Fact]1256 function UsesLocaleInLocaleProviderIfThirdParamIsBoolean() {1257 // Arrange1258 var actual;1259 var targetService = new Aura.Services.AuraLocalizationService();1260 targetService.getAvailableMomentLocale = function(locale) {1261 actual = locale;1262 }1263 var momentDate = {1264 isValid: function() {1265 return true;1266 },1267 toDate: function() {}1268 };1269 targetService.moment = {1270 utc: function() {1271 return momentDate;1272 }1273 };1274 var expected = "locale";1275 var mockUtil = Mocks.GetMock(Object.Global(), "$A", {1276 util: {1277 isBoolean: function(value) { return typeof value === "boolean" },1278 isUndefined: function(value) { return value === undefined; }1279 },1280 get: function(value){1281 if(value === "$Locale.langLocale") return expected;1282 }1283 });1284 // Act1285 mockUtil(function(){1286 targetService.parseDateTimeUTC("date", "format", true);1287 });1288 // Assert1289 Assert.Equal(expected, actual);1290 }1291 [Fact]1292 function UsesLocaleInLocaleProviderIfThirdParamIsUndefined() {1293 // Arrange1294 var actual;1295 var targetService = new Aura.Services.AuraLocalizationService();1296 targetService.getAvailableMomentLocale = function(locale) {1297 actual = locale;1298 }1299 var momentDate = {1300 isValid: function() {1301 return true;1302 },1303 toDate: function() {}1304 };1305 targetService.moment = {1306 utc: function() {1307 return momentDate;1308 }1309 };1310 var expected = "locale";1311 var mockUtil = Mocks.GetMock(Object.Global(), "$A", {1312 util: {1313 isBoolean: function(value) { return typeof value === "boolean" },1314 isUndefined: function(value) { return value === undefined; }1315 },1316 get: function(value){1317 if(value === "$Locale.langLocale") return expected;1318 }1319 });1320 // Act1321 mockUtil(function(){1322 targetService.parseDateTimeUTC("date", "format");1323 });1324 // Assert1325 Assert.Equal(expected, actual);1326 }1327 }1328 [Fixture]1329 function parseDateTimeISO8601(){1330 var createTargetService = function() {1331 var targetService = new Aura.Services.AuraLocalizationService();1332 var mockInvalidDate = {1333 isValid: function(){1334 return false;1335 }1336 };1337 targetService.moment = function(dateTimeString) {1338 if(dateTimeString === targetDateTime) return mockDateTime;1339 if(dateTimeString === "null") return null;1340 return mockInvalidDate;1341 };1342 return targetService;1343 }1344 [Fact]1345 function ReturnsNullForEmptyDateTimeString(){1346 // Arrange1347 var targetService = createTargetService();1348 // Act1349 var actual = targetService.parseDateTimeISO8601("");1350 // Assert1351 Assert.Null(actual);1352 }1353 [Fact]1354 function ReturnsNullWhenMomentReturnsNull(){1355 // Arrange1356 var expected = null;1357 var targetService = createTargetService();1358 // Act1359 var actual = targetService.parseDateTimeISO8601("null");1360 // Assert1361 Assert.Null(actual);1362 }1363 [Fact]1364 function ReturnsDateTimeForValidDateTime(){1365 // Arrange1366 var expected = targetDateTime;1367 var targetService = createTargetService();1368 // Act1369 var actual = targetService.parseDateTimeISO8601(targetDateTime);1370 // Assert1371 Assert.Equal(expected, actual);1372 }1373 }1374 [Fixture]1375 function toISOString() {1376 [Fact]1377 function ReturnsInputValueForFalsyInput() {1378 // Arrange1379 var expected = "";1380 var targetService = new Aura.Services.AuraLocalizationService();1381 // Act1382 var actual = targetService.toISOString(expected);1383 // Assert1384 Assert.Equal(expected, actual);1385 }1386 [Fact]1387 function ReturnsInputValueForNonDateInput() {1388 // Arrange1389 var expected = "2015-04-01";1390 var targetService = new Aura.Services.AuraLocalizationService();1391 var actual;1392 // Act1393 mockUtil(function() {1394 actual = targetService.toISOString(expected);1395 });1396 // Assert1397 Assert.Equal(expected, actual);1398 }1399 [Fact]1400 function CallsToISOStringOnDateIfExits() {1401 // Arrange1402 var targetService = new Aura.Services.AuraLocalizationService();1403 var date = new Date();1404 date.toISOString = Stubs.GetMethod();1405 // Act1406 mockUtil(function() {1407 targetService.toISOString(date);1408 });1409 // Assert1410 Assert.Equal(1, date.toISOString.Calls.length);1411 }1412 [Fact]1413 function ReturnsISOStringForDateWithToISOString(){1414 // Arrange1415 var targetService = new Aura.Services.AuraLocalizationService();1416 var expected = "2004-11-09T12:30:59.123Z";1417 var date = new Date(Date.UTC(2004,10,09,12,30,59,123));1418 var actual;1419 // Act1420 mockUtil(function() {1421 actual = targetService.toISOString(date);1422 });1423 // Assert1424 Assert.Equal(expected, actual);1425 }1426 [Fact]1427 function ReturnsISOStringForDateWithoutToISOString(){1428 // Arrange1429 var targetService = new Aura.Services.AuraLocalizationService();1430 var expected = "2004-11-09T12:30:59.123Z";1431 var date = new Date(Date.UTC(2004,10,09,12,30,59,123));1432 date.toISOString = undefined;1433 var actual;1434 // Act1435 mockUtil(function() {1436 actual = targetService.toISOString(date);1437 });1438 // Assert1439 Assert.Equal(expected, actual);1440 }1441 }1442 [Fixture]1443 function UTCToWallTime() {1444 var createTargetService = function() {1445 var targetService = new Aura.Services.AuraLocalizationService();1446 targetService.moment = {1447 tz: {1448 zone: function(timezone) {1449 if (timezone === "unsupported") {1450 return null;1451 }1452 return Stubs.GetObject({});1453 }1454 }1455 };1456 return targetService;1457 }1458 [Fact]1459 function CallbackWithOriginalDateWhenTimezoneLibIsMissing() {1460 // Arrange1461 var targetService = new Aura.Services.AuraLocalizationService();1462 var expected = Stubs.GetObject({});1463 var actual;1464 targetService.moment = {};1465 // Act1466 mockUtil(function () {1467 targetService.UTCToWallTime(expected, "GMT", function(date) {1468 actual = date;1469 });1470 });1471 // Assert1472 Assert.Equal(expected, actual);1473 }1474 [Fact]1475 function CallbackWithOriginalDateWhenZoneIsGMT() {1476 // Arrange1477 var targetService = createTargetService();1478 var expected = Stubs.GetObject({});1479 var actual;1480 // Act1481 mockUtil(function () {1482 targetService.UTCToWallTime(expected, "GMT", function(date) {1483 actual = date;1484 });1485 });1486 // Assert1487 Assert.Equal(expected, actual);1488 }1489 [Fact]1490 function UsesUserTimezoneIfTimeZoneIsFalsy() {1491 // Arrange1492 var targetService = createTargetService();1493 var expected = targetDateTime;1494 var actual;1495 var mockUtil = Mocks.GetMock(Object.Global(), "$A", {1496 get:function(value){1497 if(value === "$Locale.timezone") return "UTC";1498 },1499 assert: function () {},1500 warning: function() {}1501 });1502 // Act1503 mockUtil(function() {1504 // when timezone is UTC, callback gets called with the original date1505 targetService.UTCToWallTime(mockDateTime, undefined, function(date) {1506 actual = date;1507 });1508 });1509 // Assert1510 Assert.Equal(expected, actual);1511 }1512 [Fact]1513 function UsesUserTimezoneIfTimeZoneIsUnsupported(){1514 // Arrange1515 var targetService = createTargetService();1516 var expected = targetDateTime;1517 var actual;1518 var mockUtil = Mocks.GetMock(Object.Global(), "$A", {1519 get:function(value){1520 if(value === "$Locale.timezone") return "UTC";1521 },1522 assert: function () {},1523 warning: function() {}1524 });1525 // Act1526 mockUtil(function() {1527 // when timezone is UTC, callback gets called with the original date1528 targetService.UTCToWallTime(mockDateTime, "unsupported", function(date) {1529 actual = date;1530 });1531 });1532 // Assert1533 Assert.Equal(expected, actual);1534 }1535 }1536 [Fixture]1537 function WallTimeToUTC(){1538 var createTargetService = function() {1539 var targetService = new Aura.Services.AuraLocalizationService();1540 targetService.moment = {1541 tz: {1542 zone: function(timezone) {1543 if (timezone === "unsupported") {1544 return null;1545 }1546 return Stubs.GetObject({});1547 }1548 }1549 };1550 return targetService;1551 }1552 [Fact]1553 function CallbackWithOriginalDateWhenTimezoneLibIsMissing() {1554 // Arrange1555 var targetService = new Aura.Services.AuraLocalizationService();1556 var expected = Stubs.GetObject({});1557 var actual;1558 targetService.moment = {};1559 // Act1560 mockUtil(function () {1561 targetService.WallTimeToUTC(expected, "GMT", function(date) {1562 actual = date;1563 });1564 });1565 // Assert1566 Assert.Equal(expected, actual);1567 }1568 [Fact]1569 function CallbackWithOriginalDateWhenZoneIsGMT() {1570 // Arrange1571 var targetService = createTargetService();1572 var expected = Stubs.GetObject({});1573 var actual;1574 // Act1575 mockUtil(function () {1576 targetService.WallTimeToUTC(expected, "GMT", function(date) {1577 actual = date;1578 });1579 });1580 // Assert1581 Assert.Equal(expected, actual);1582 }1583 [Fact]1584 function UsesUserTimezoneIfTimeZoneIsFalsy(){1585 // Arrange1586 var targetService = createTargetService();1587 var expected = targetDateTime;1588 var actual;1589 var mockUtil = Mocks.GetMock(Object.Global(), "$A", {1590 get:function(value){1591 if(value === "$Locale.timezone") return "UTC";1592 },1593 assert: function () {},1594 warning: function() {}1595 });1596 // Act1597 mockUtil(function() {1598 // when timezone is UTC, callback gets called with the original date1599 targetService.WallTimeToUTC(mockDateTime, undefined, function(date) {1600 actual = date;1601 });1602 });1603 // Assert1604 Assert.Equal(expected, actual);1605 }1606 [Fact]1607 function UsesUserTimezoneIfTimeZoneIsUnsupported(){1608 // Arrange1609 var targetService = createTargetService();1610 var expected = targetDateTime;1611 var actual;1612 var mockUtil = Mocks.GetMock(Object.Global(), "$A", {1613 get:function(value){1614 if(value === "$Locale.timezone") return "UTC";1615 },1616 assert: function () {},1617 warning: function() {}1618 });1619 // Act1620 mockUtil(function() {1621 // when timezone is UTC, callback gets called with the original date1622 targetService.WallTimeToUTC(mockDateTime, "unsupported", function(date) {1623 actual = date;1624 });1625 });1626 // Assert1627 Assert.Equal(expected, actual);1628 }1629 }1630 [Fixture]1631 function displayDateTime(){1632 var targetFormat = "format";1633 var targetLang = "lang";1634 var targetDateTimeObj={1635 l:'',1636 f:'',1637 locale:function(lang){1638 if(lang === targetLang) this.l = lang;1639 },1640 format:function(format){1641 if(format === targetFormat) this.f = format + this.l;1642 return this.f;1643 }1644 };1645 var createTargetService = function() {1646 var targetService = new Aura.Services.AuraLocalizationService();1647 //targetService.moment =1648 targetService.getNormalizedFormat = function(format){1649 return format;1650 }1651 targetService.getAvailableMomentLocale = function(locale) {1652 return locale;1653 }1654 return targetService;1655 };1656 [Fact]1657 function InvalidLocale(){1658 // Arrange1659 var targetService = createTargetService();1660 var expected = targetFormat;1661 // Act1662 var actual = targetService.displayDateTime(targetDateTimeObj, targetFormat, '');1663 // Assert1664 Assert.Equal(expected, actual);1665 }1666 [Fact]1667 function validFormatAndLocale(){1668 // Arrange1669 var targetService = createTargetService();1670 var expected = targetFormat+targetLang;1671 // Act1672 var actual = targetService.displayDateTime(targetDateTimeObj, targetFormat, targetLang);1673 // Assert1674 Assert.Equal(expected, actual);1675 }1676 }1677 [Fixture]1678 function getNormalizedFormat(){1679 var targetFormat = "DDMMYYYY";1680 [Fact]1681 function inValidFormat(){1682 // Arrange1683 var expected = "";1684 var actual;1685 // Act1686 actual = targetService.getNormalizedFormat("");1687 // Assert1688 Assert.Equal(expected, actual);1689 }1690 [Fact]1691 function cacheHit(){1692 // Arrange1693 var expected = targetFormat;1694 var actual;1695 var mockCache = Mocks.GetMock(targetService, "cache", {1696 format: {1697 DDMMYYYY:targetFormat1698 }1699 });1700 // Act1701 mockCache(function(){1702 actual = targetService.getNormalizedFormat(targetFormat);1703 });1704 // Assert1705 Assert.Equal(expected, actual);1706 }1707 [Fact]1708 function cacheMiss(){1709 // Arrange1710 var expected = targetFormat;1711 var actual;1712 var mockCache = Mocks.GetMock(targetService, "cache", {1713 format: {1714 ddMMyyyy:false1715 }1716 });1717 // Act1718 mockCache(function(){1719 actual = targetService.getNormalizedFormat("ddMMyyyy");1720 });1721 // Assert1722 Assert.Equal(expected, actual);1723 }1724 }1725 [Fixture]1726 function getStrictModeFormat(){1727 var targetFormat = "DDMMYYYY";1728 [Fact]1729 function emptyFormat(){1730 // Arrange1731 var expected = "";1732 var actual;1733 // Act1734 actual = targetService.getStrictModeFormat("");1735 // Assert1736 Assert.Equal(expected, actual);1737 }1738 [Fact]1739 function dateOnlySingleLetterFormat(){1740 // Arrange1741 var expected = "D-M-YYYY";1742 var actual;1743 // Act1744 actual = targetService.getStrictModeFormat("d-M-y");1745 // Assert1746 Assert.Equal(expected, actual);1747 }1748 [Fact]1749 function dateOnlyDoubleLetterFormat(){1750 // Arrange1751 var expected = "D-M-YYYY";1752 var actual;1753 // Act1754 actual = targetService.getStrictModeFormat("dd-MM-y");1755 // Assert1756 Assert.Equal(expected, actual);1757 }1758 [Fact]1759 function dateTimeSingleLetterFormat(){1760 // Arrange1761 var expected = "D-M-YYYY h:m A";1762 var actual;1763 // Act1764 actual = targetService.getStrictModeFormat("d-M-y h:m a");1765 // Assert1766 Assert.Equal(expected, actual);1767 }1768 [Fact]1769 function dateTimeDoubleLetterFormat(){1770 // Arrange1771 var expected = "D-M-YYYY h:m A";1772 var actual;1773 // Act1774 actual = targetService.getStrictModeFormat("dd-MM-y hh:mm a");1775 // Assert1776 Assert.Equal(expected, actual);1777 }1778 [Fact]1779 function dateTimeNoSpaceAMPMFormat(){1780 // Arrange1781 var expected = "D-M-YYYY h:m A";1782 var actual;1783 // Act1784 actual = targetService.getStrictModeFormat("dd-MM-y hh:mmA ");1785 // Assert1786 Assert.Equal(expected, actual);1787 }1788 [Fact]1789 function dateTimeExtraSpaceAMPMFormat(){1790 // Arrange1791 var expected = "D-M-YYYY h:m A";1792 var actual;1793 // Act1794 actual = targetService.getStrictModeFormat("dd-MM-y hh:mm A ");1795 // Assert1796 Assert.Equal(expected, actual);1797 }1798 [Fact]1799 function dateTime24HrFormat(){1800 // Arrange1801 var expected = "D-M-YYYY H:m";1802 var actual;1803 // Act1804 actual = targetService.getStrictModeFormat("d-M-y HH:mm");1805 // Assert1806 Assert.Equal(expected, actual);1807 }1808 }1809 [Fixture]1810 function getAvailableMomentLocale(){1811 [Fact]1812 function ReturnsValueInCacheIfHits() {1813 // Arrange1814 var expected = "momentLocale";1815 var targetService = new Aura.Services.AuraLocalizationService();1816 targetService.localeCache = { "javaLocale": expected };1817 // Act1818 var actual = targetService.getAvailableMomentLocale("javaLocale");1819 // Assert1820 Assert.Equal(expected, actual);1821 }1822 [Fact]1823 function ReturnsEnIfCacheMisses() {1824 // Arrange1825 var expected = "en";1826 var targetService = new Aura.Services.AuraLocalizationService();1827 // Act1828 var actual = targetService.getAvailableMomentLocale("notExists");1829 // Assert1830 Assert.Equal(expected, actual);1831 }1832 }1833 [Fixture]1834 function normalizeToMomentLocale(){1835 [Fact]1836 function ReturnsArgValueForFalsy() {1837 // Arrange1838 var expected = "";1839 var targetService = new Aura.Services.AuraLocalizationService();1840 // Act1841 var actual = targetService.normalizeToMomentLocale(expected);1842 // Assert1843 Assert.Equal(expected, actual);1844 }1845 [Fact]1846 function ReturnsMomentLocaleIfCompoundLocaleMatches(){1847 // Arrange1848 var expected = "zh-cn";1849 var targetService = new Aura.Services.AuraLocalizationService();1850 targetService.moment = {1851 locales: function(){1852 return [expected];1853 }1854 };1855 // Act1856 var actual = targetService.normalizeToMomentLocale("zh_CN");1857 // Assert1858 Assert.Equal(expected, actual);1859 }1860 [Fact]1861 function ReturnsMomentLocaleIfLanguageLocaleMatches(){1862 // Arrange1863 var expected = "de";1864 var targetService = new Aura.Services.AuraLocalizationService();1865 targetService.moment = {1866 locales: function(){1867 return [expected];1868 }1869 };1870 // Act1871 var actual = targetService.normalizeToMomentLocale("de_DE");1872 // Assert1873 Assert.Equal(expected, actual);1874 }1875 [Fact]1876 function ReturnEnIfNoMatchingLocale(){1877 // Arrange1878 var expected = "en";1879 var targetService = new Aura.Services.AuraLocalizationService();1880 targetService.moment = {1881 locales: function(){1882 return [];1883 }1884 };1885 // Act1886 var actual = targetService.normalizeToMomentLocale("not_matching");1887 // Assert1888 Assert.Equal(expected, actual);1889 }1890 }1891 [Fixture]1892 function isAvailableLocale(){1893 [Fact]1894 function ReturnsFalseForFalsyValue() {1895 // Arrange1896 var targetService = new Aura.Services.AuraLocalizationService();1897 // Act1898 var actual = targetService.isAvailableLocale("");1899 // Assert1900 Assert.False(actual);1901 }1902 [Fact]1903 function ReturnsTrueIfLocaleCacheHits(){1904 // Arrange1905 var targetService = new Aura.Services.AuraLocalizationService();1906 targetService.localeCache["zh_CN"] = "zh-cn";1907 // Act1908 var actual = targetService.isAvailableLocale("zh_CN");1909 // Assert1910 Assert.True(actual);1911 }1912 [Fact]1913 function ReturnsTrueIfMomentDataAvailable(){1914 // Arrange1915 var targetService = new Aura.Services.AuraLocalizationService();1916 targetService.moment = {1917 locales: function(){1918 return ["zh-cn"];1919 }1920 };1921 // Act1922 var actual = targetService.isAvailableLocale("ZH-CN");1923 // Assert1924 Assert.True(actual);1925 }1926 [Fact]1927 function AddsToCacheIfCacheMissesOnAvailableLocale(){1928 // Arrange1929 var expected = "zh-cn";1930 var locale = "zh_CN";1931 var targetService = new Aura.Services.AuraLocalizationService();1932 targetService.moment = {1933 locales: function(){1934 return [expected];1935 }1936 };1937 // Act1938 targetService.isAvailableLocale(locale);1939 var actual = targetService.localeCache[locale];1940 // Assert1941 Assert.Equal(expected, actual);1942 }1943 [Fact]1944 function ReturnsFalseForUnavailableLocale(){1945 // Arrange1946 var targetService = new Aura.Services.AuraLocalizationService();1947 targetService.moment = {1948 locales: function(){1949 return [];1950 }1951 };1952 // Act1953 var actual = targetService.isAvailableLocale("unavailable");1954 // Assert1955 Assert.False(actual);1956 }1957 [Fact]1958 function ReturnsTrueIfEnLocaleIsAvailable(){1959 var targetService = new Aura.Services.AuraLocalizationService();1960 targetService.moment = {1961 locales: function(){1962 return ["en"];1963 }1964 };1965 // Act1966 var actual = targetService.isAvailableLocale("en-US");1967 // Assert1968 Assert.True(actual);1969 }1970 }1971 [Fixture]1972 function init(){1973 [Fact]1974 function SetsMomentLocaleAsValueInLocaleProvider(){1975 // Arrange1976 var expected = "locale";1977 var javaLocale = "JavaLocale";1978 var targetService = new Aura.Services.AuraLocalizationService();1979 var actual;1980 targetService.normalizeToMomentLocale = function(locale) {1981 if (locale === javaLocale) { return expected; }1982 }1983 var mockUtil = Mocks.GetMock(Object.Global(), "$A", {1984 get: function(key) {1985 if(key === "$Locale.langLocale") return javaLocale;1986 }1987 });1988 var mockMoment = Mocks.GetMock(Object.Global(), "moment", {1989 locale: function(locale) {1990 actual = locale;1991 }1992 });1993 // Act1994 mockUtil(function(){1995 mockMoment(function(){1996 targetService.init();1997 });1998 });1999 // Assert2000 Assert.Equal(expected, actual);2001 }2002 [Fact]2003 function AddsAvailableLocaleToCache(){2004 // Arrange2005 var expected = "locale";2006 var javaLocale = "JavaLocale";2007 var targetService = new Aura.Services.AuraLocalizationService();2008 targetService.normalizeToMomentLocale = function(locale) {2009 if (locale === javaLocale) { return expected; }2010 }2011 var mockUtil = Mocks.GetMock(Object.Global(), "$A", {2012 get: function(key) {2013 if(key === "$Locale.langLocale") return javaLocale;2014 }2015 });2016 var mockMoment = Mocks.GetMock(Object.Global(), "moment", {2017 locale: function(locale) {}2018 });2019 // Act2020 mockUtil(function(){2021 mockMoment(function(){2022 targetService.init();2023 });2024 });2025 // Assert2026 var actual = targetService.localeCache[javaLocale];2027 Assert.Equal(expected, actual);2028 }2029 }2030 [Fixture]2031 function formatNumbers(){2032 [Fact]2033 function formatNumber(){2034 // Arrange2035 var expected = targetNumber;2036 var actual;2037 var mockGetDefaultNumberFormat = Mocks.GetMock(targetService, "getDefaultNumberFormat", function(){2038 return {2039 format: function(number){2040 if(number === targetNumber) return targetNumber;2041 }2042 };2043 });2044 // Act2045 mockGetDefaultNumberFormat(function(){2046 actual = targetService.formatNumber(targetNumber);2047 });2048 // Assert2049 Assert.Equal(expected, actual);2050 }2051 [Fact]2052 function formatPercent(){2053 // Arrange2054 var expected = targetPercent;2055 var actual;2056 var mockGetDefaultPercentFormat = Mocks.GetMock(targetService, "getDefaultPercentFormat", function(){2057 return {2058 format: function(number){2059 if(number === targetPercent) return targetPercent;2060 }2061 };2062 });2063 // Act2064 mockGetDefaultPercentFormat(function(){2065 actual = targetService.formatPercent(targetPercent);2066 });2067 // Assert2068 Assert.Equal(expected, actual);2069 }2070 [Fact]2071 function formatCurrency(){2072 // Arrange2073 var expected = targetCurrency;2074 var actual;2075 var mockGetDefaultCurrencyFormat = Mocks.GetMock(targetService, "getDefaultCurrencyFormat", function(){2076 return {2077 format: function(number){2078 if(number === targetCurrency) return targetCurrency;2079 }2080 };2081 });2082 // Act2083 mockGetDefaultCurrencyFormat(function(){2084 actual = targetService.formatCurrency(targetCurrency);2085 });2086 // Assert2087 Assert.Equal(expected, actual);2088 }2089 }2090 [Fixture]2091 function getNumberFormat(){2092 [Fact]2093 function getNumberFormat(){2094 // Arrange2095 var actual;2096 var targetSymbols = '###';2097 var mockNumberFormat = {};2098 var mockNumberFormatConstructor = Mocks.GetMock(Object.Global(), "Aura", { "Utils" : {"NumberFormat": function(format, symbols){2099 if(format === targetNumberFormat && symbols === targetSymbols) return mockNumberFormat;2100 }}});2101 // Act2102 mockNumberFormatConstructor(function(){2103 actual = targetService.getNumberFormat(targetNumberFormat, targetSymbols);2104 });2105 // Assert2106 Assert.Equal(mockNumberFormat, actual);2107 }2108 }2109 [Fixture]2110 function getDefaultFormats(){2111 [Fact]2112 function getNumberFormat(){2113 // Arrange2114 var actual;2115 var mockNumberFormat = {};2116 var mockNumberFormatConstructor = Mocks.GetMock(Object.Global(), "Aura", { "Utils" : {"NumberFormat": function(val){2117 if(val === targetNumberFormat) return mockNumberFormat;2118 }}});2119 // Act2120 mockUtil(function(){2121 mockNumberFormatConstructor(function(){2122 actual = targetService.getDefaultNumberFormat();2123 });2124 });2125 // Assert2126 Assert.Equal(mockNumberFormat, actual);2127 }2128 [Fact]2129 function getDefaultPercentFormat(){2130 // Arrange2131 var actual;2132 var mockNumberFormat = {};2133 var mockNumberFormatConstructor = Mocks.GetMock(Object.Global(), "Aura", { "Utils" : {"NumberFormat": function(val){2134 if(val === targetPercentFormat) return mockNumberFormat;2135 }}});2136 // Act2137 mockUtil(function(){2138 mockNumberFormatConstructor(function(){2139 actual = targetService.getDefaultPercentFormat();2140 });2141 });2142 // Assert2143 Assert.Equal(mockNumberFormat, actual);2144 }2145 [Fact]2146 function getDefaultCurrencyFormat(){2147 // Arrange2148 var actual;2149 var mockNumberFormat = {};2150 var mockNumberFormatConstructor = Mocks.GetMock(Object.Global(), "Aura", { "Utils" : {"NumberFormat": function(val){2151 if(val === targetCurrencyFormat) return mockNumberFormat;2152 }}});2153 // Act2154 mockUtil(function(){2155 mockNumberFormatConstructor(function(){2156 actual = targetService.getDefaultCurrencyFormat();2157 });2158 });2159 // Assert2160 Assert.Equal(mockNumberFormat, actual);2161 }2162 }2163 [Fixture]2164 function pad(){2165 [Fact]2166 function pad0(){2167 // Arrange2168 var expected = '00';2169 var actual;2170 // Act2171 actual = targetService.pad(0);2172 // Assert2173 Assert.Equal(expected, actual);2174 }2175 [Fact]2176 function pad1(){2177 // Arrange2178 var expected = '01';2179 var actual;2180 // Act2181 actual = targetService.pad(1);2182 // Assert2183 Assert.Equal(expected, actual);2184 }2185 [Fact]2186 function pad9(){2187 // Arrange2188 var expected = '09';2189 var actual;2190 // Act2191 actual = targetService.pad(9);2192 // Assert2193 Assert.Equal(expected, actual);2194 }2195 [Fact]2196 function pad10(){2197 // Arrange2198 var expected = '10';2199 var actual;2200 // Act2201 actual = targetService.pad(10);2202 // Assert2203 Assert.Equal(expected, actual);2204 }2205 }2206 [Fixture]2207 function doublePad(){2208 [Fact]2209 function pad0(){2210 // Arrange2211 var expected = '000';2212 var actual;2213 // Act2214 actual = targetService.doublePad(0);2215 // Assert2216 Assert.Equal(expected, actual);2217 }2218 [Fact]2219 function pad1(){2220 // Arrange2221 var expected = '001';2222 var actual;2223 // Act2224 actual = targetService.doublePad(1);2225 // Assert2226 Assert.Equal(expected, actual);2227 }2228 [Fact]2229 function pad9(){2230 // Arrange2231 var expected = '099';2232 var actual;2233 // Act2234 actual = targetService.doublePad(99);2235 // Assert2236 Assert.Equal(expected, actual);2237 }2238 [Fact]2239 function pad10(){2240 // Arrange2241 var expected = '100';2242 var actual;2243 // Act2244 actual = targetService.doublePad(100);2245 // Assert2246 Assert.Equal(expected, actual);2247 }2248 }...

Full Screen

Full Screen

AuraStorageServiceTest.js

Source:AuraStorageServiceTest.js Github

copy

Full Screen

1/*2 * Copyright (C) 2013 salesforce.com, inc.3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16Function.RegisterNamespace("Test.Aura.Storage");17[Fixture]18Test.Aura.Storage.AuraStorageServiceTest = function() {19 var $A = {};20 var Aura = {Services: {} };21 Mocks.GetMocks(Object.Global(), {22 "$A": $A,23 "Aura": Aura,24 "AuraStorageService": function() {}25 })(function () {26 Import("aura-impl/src/main/resources/aura/storage/AuraStorageService.js");27 });28 var mockOnLoadUtil = Mocks.GetMocks(Object.Global(), {29 "$A": $A,30 Aura: Aura31 });32 [Fixture]33 function initStorage() {34 var targetService;35 mockOnLoadUtil(function() {36 targetService = new Aura.Services.AuraStorageService();37 // mock initStorage() calls to select and get the adapter class38 targetService.selectAdapter = function() { return "fake"; };39 targetService.adapters["fake"] = { adapterClass: function(){} };40 });41 var configPassedToAuraStorage;42 var mockA = Mocks.GetMocks(Object.Global(), {43 "$A": {44 assert: function() {},45 util: {46 isBoolean: function(obj) { return typeof obj === "boolean"; },47 isFiniteNumber: function(obj) { return typeof obj === "number" && isFinite(obj); },48 isString: function(obj) { return typeof obj === "string"; },49 isObject: function(obj) { return typeof obj === "object" && obj !== null && !Array.isArray(obj); }50 }51 },52 "AuraStorage": function(config) { configPassedToAuraStorage = config; }53 });54 [Fact]55 function RequiresConfig() {56 var expected = new Error("expected");57 var actual;58 var mockAssert = Mocks.GetMock(Object.Global(), "$A", {59 assert: function(value, msg) {60 if (!value) {61 throw expected;62 }63 },64 util: {65 isString: function(obj) { return typeof obj === "string"; },66 isObject: function(obj) { return typeof obj === "object" && obj !== null && !Array.isArray(obj); }67 }68 });69 mockAssert(function() {70 try {71 targetService.initStorage();72 Assert.Fail("Error should've been thrown");73 } catch (e) {74 actual = e;75 }76 });77 Assert.Equal(expected, actual);78 }79 [Fact]80 function RequiresTruthyName() {81 var expected = new Error("expected");82 var actual;83 var mockAssert = Mocks.GetMock(Object.Global(), "$A", {84 assert: function(value, msg) {85 if (!value) {86 throw expected;87 }88 },89 util: {90 isString: function(obj) { return typeof obj === "string"; },91 isObject: function(obj) { return typeof obj === "object" && obj !== null && !Array.isArray(obj); }92 }93 });94 mockAssert(function() {95 try {96 targetService.initStorage({});97 Assert.Fail("Error should've been thrown");98 } catch (e) {99 actual = e;100 }101 });102 Assert.Equal(expected, actual);103 }104 [Fact]105 function NotInitDuplicateStorages() {106 var storageName = "testStorage";107 var expected = "Storage named 'testStorage' already exists";108 var actual;109 var mockAssert = Mocks.GetMock(Object.Global(), "$A", {110 assert: function(value, msg) {111 if (!value) {112 throw new Error(msg);113 }114 },115 util: {116 isString: function(obj) { return typeof obj === "string"; },117 isObject: function(obj) { return typeof obj === "object" && obj !== null && !Array.isArray(obj); }118 }119 });120 targetService.storages[storageName] = true;121 mockAssert(function() {122 try {123 targetService.initStorage( {name:storageName} );124 Assert.Fail("Error should've been thrown");125 } catch (e) {126 actual = e.toString();127 }128 });129 Assert.Equal(expected, actual);130 }131 [Fact]132 function ReturnsCreatedStorage() {133 var mockStorage = Mocks.GetMocks(Object.Global(), {134 "$A": {135 assert: function() {},136 util: {137 isBoolean: function(obj) { return typeof obj === "boolean"; },138 isFiniteNumber: function(obj) { return typeof obj === "number" && isFinite(obj); },139 isString: function(obj) { return typeof obj === "string"; },140 isObject: function(obj) { return typeof obj === "object" && obj !== null && !Array.isArray(obj); }141 }142 },143 "AuraStorage": function(config) {144 return { name: config["name"] };145 }146 });147 var expected = "name";148 var storage;149 mockStorage(function() {150 storage = targetService.initStorage({name:expected});151 });152 Assert.Equal(expected, storage["name"]);153 }154 [Fact]155 function AddsCreatedStorageToStorages() {156 var mockStorage = Mocks.GetMocks(Object.Global(), {157 "$A": {158 assert: function() {},159 util: {160 isBoolean: function(obj) { return typeof obj === "boolean"; },161 isFiniteNumber: function(obj) { return typeof obj === "number" && isFinite(obj); },162 isString: function(obj) { return typeof obj === "string"; },163 isObject: function(obj) { return typeof obj === "object" && obj !== null && !Array.isArray(obj); }164 }165 },166 "AuraStorage": function(config) {167 return { name: config["name"] };168 }169 });170 var expected = "name";171 var storage;172 mockStorage(function() {173 targetService.initStorage({name:expected});174 storage = targetService.getStorage(expected);175 });176 Assert.Equal(expected, storage["name"]);177 }178 [Fact]179 function NameRespectsArgument() {180 var expected = "name";181 mockA(function() {182 targetService.initStorage({name:expected});183 });184 Assert.Equal(expected, configPassedToAuraStorage.name);185 }186 [Fact]187 function PersistentDefaultsFalse() {188 mockA(function() {189 targetService.initStorage({name:"name"});190 });191 Assert.False(configPassedToAuraStorage.persistent);192 }193 [Fact]194 function PersistentRespectsArgument() {195 mockA(function() {196 targetService.initStorage({name:"name", persistent:true});197 });198 Assert.True(configPassedToAuraStorage.persistent);199 }200 [Fact]201 function SecureDefaultsFalse() {202 mockA(function() {203 targetService.initStorage({name:"name"});204 });205 Assert.False(configPassedToAuraStorage.secure);206 }207 [Fact]208 function SecureRespectsArgument() {209 mockA(function() {210 targetService.initStorage({name:"name", secure:true});211 });212 Assert.True(configPassedToAuraStorage.secure);213 }214 [Fact]215 function UndefinedMaxSizeDefaults1MB() {216 mockA(function() {217 targetService.initStorage({name:"name"});218 });219 Assert.Equal(1000*1024, configPassedToAuraStorage.maxSize);220 }221 [Fact]222 function ZeroMaxSizeDefaults1MB() {223 mockA(function() {224 targetService.initStorage({name:"name", maxSize:0});225 });226 Assert.Equal(1000*1024, configPassedToAuraStorage.maxSize);227 }228 [Fact]229 function MaxSizeRespectsArgument() {230 var expected = 25;231 mockA(function() {232 targetService.initStorage({name:"name", maxSize:expected});233 });234 Assert.Equal(expected, configPassedToAuraStorage.maxSize);235 }236 [Fact]237 function UndefinedExpirationDefaults10() {238 mockA(function() {239 targetService.initStorage({name:"name"});240 });241 Assert.Equal(10, configPassedToAuraStorage.expiration);242 }243 [Fact]244 function NegativeExpirationDefaults10() {245 mockA(function() {246 targetService.initStorage({name:"name", expiration:-1});247 });248 Assert.Equal(10, configPassedToAuraStorage.expiration);249 }250 [Fact]251 function ExpirationRespectsArgument() {252 var expected = 25;253 mockA(function() {254 targetService.initStorage({name:"name",expiration:expected});255 });256 Assert.Equal(expected, configPassedToAuraStorage.expiration);257 }258 [Fact]259 function UndefinedAutoRefreshIntervalDefaults30() {260 mockA(function() {261 targetService.initStorage({name:"name"});262 });263 Assert.Equal(30, configPassedToAuraStorage.autoRefreshInterval);264 }265 [Fact]266 function NegativeAutoRefreshIntervalDefaults30() {267 mockA(function() {268 targetService.initStorage({name:"name", autoRefreshInterval:-1});269 });270 Assert.Equal(30, configPassedToAuraStorage.autoRefreshInterval);271 }272 [Fact]273 function AutoRefreshIntervalRespectsArgument() {274 var expected = 25;275 mockA(function() {276 targetService.initStorage({name:"name", autoRefreshInterval:expected});277 });278 Assert.Equal(expected, configPassedToAuraStorage.autoRefreshInterval);279 }280 [Fact]281 function DebugLoggingDefaultsFalse() {282 mockA(function() {283 targetService.initStorage({name:"name"});284 });285 Assert.False(configPassedToAuraStorage.debugLogging);286 }287 [Fact]288 function DebugLoggingRespectsArgument() {289 mockA(function() {290 targetService.initStorage({name:"name", debugLogging:true});291 });292 Assert.True(configPassedToAuraStorage.debugLogging);293 }294 [Fact]295 function ClearOnInitDefaultsTrue() {296 mockA(function() {297 targetService.initStorage({name:"name"});298 });299 Assert.True(configPassedToAuraStorage.clearOnInit);300 }301 [Fact]302 function ClearOnInitRespectsArgument() {303 mockA(function() {304 targetService.initStorage({name:"name", clearOnInit:false});305 });306 Assert.False(configPassedToAuraStorage.clearOnInit);307 }308 [Fact]309 function VersionDefault() {310 var expected = "expected";311 targetService.version = expected;312 mockA(function() {313 targetService.initStorage({name:"name"});314 });315 Assert.Equal(expected, configPassedToAuraStorage.version);316 }317 [Fact]318 function VersionRespectsArgument() {319 var expected = "version";320 mockA(function() {321 targetService.initStorage({name:"name", version:expected});322 });323 Assert.Equal(expected, configPassedToAuraStorage.version);324 }325 [Fact]326 function VersionDefaultForFalsyValue() {327 var expected = "expected";328 targetService.version = expected;329 mockA(function() {330 targetService.initStorage({name:"name", version:""});331 });332 Assert.Equal(expected, configPassedToAuraStorage.version);333 }334 [Fact]335 function IsolationKeyNotUseConfigProperty() {336 var expected = "expected";337 targetService.isolationKey = expected;338 mockA(function() {339 targetService.initStorage({name:"name", isolationKey:"shouldIgnore"});340 });341 Assert.Equal(expected, configPassedToAuraStorage.isolationKey);342 }343 [Fact]344 function PartitionNameNotUseConfigProperty() {345 var expected = "expected";346 targetService.partitionName = expected;347 mockA(function() {348 targetService.initStorage({name:"name", partitionName:"shouldIgnore"});349 });350 Assert.Equal(expected, configPassedToAuraStorage.partitionName);351 }352 }353 [Fixture]354 function selectAdapter() {355 var mockA = Mocks.GetMocks(Object.Global(), {356 "Aura": {357 "Storage": {358 "MemoryAdapter": {359 "NAME": "memory"360 }361 }362 },363 "$A": {364 assert: function(condition, assertMessage) {365 if (!condition) { throw new Error(assertMessage); }366 },367 util: {368 isString: function(obj) { return typeof obj === "string"; },369 isFunction: function(obj) { return true; }370 }371 },372 });373 function registerAdapter(service, name, persistent, secure) {374 service.registerAdapter({375 name: name,376 persistent: persistent,377 secure: secure,378 adapterClass: function(){}379 });380 }381 [Fact]382 function SecureRespectedIfPossible() {383 var expected = "PersistentFalseSecureTrue";384 var actual;385 mockA(function() {386 var targetService = new Aura.Services.AuraStorageService();387 registerAdapter(targetService, "PersistentTrueSecureFalse", true, false)388 registerAdapter(targetService, expected, false, true);389 actual = targetService.selectAdapter(false, true);390 });391 Assert.Equal(expected, actual);392 }393 [Fact]394 function SecureFallsBackToMemoryAdapter() {395 var actual;396 mockA(function() {397 var targetService = new Aura.Services.AuraStorageService();398 registerAdapter(targetService, "memory", false, false);399 actual = targetService.selectAdapter(false, true);400 });401 Assert.Equal("memory", actual);402 }403 [Fact]404 function SecureErrorsIfMemoryAdapterNotRegistered() {405 var expected = "Memory Aura Storage Adapter was not registered";406 var actual;407 mockA(function() {408 var targetService = new Aura.Services.AuraStorageService();409 registerAdapter(targetService, "PersistentFalseSecureFalse", false, false)410 try {411 targetService.selectAdapter(false, true);412 Assert.Fail("Error should've been thrown");413 } catch (e) {414 actual = e;415 }416 });417 Assert.Equal(expected, actual.message);418 }419 [Fact]420 function PersistentRespectedIfPossible() {421 var expected = "PersistentTrueSecureFalse";422 var actual;423 mockA(function() {424 var targetService = new Aura.Services.AuraStorageService();425 registerAdapter(targetService, "PersistentFalseSecureTrue", false, true);426 registerAdapter(targetService, expected, true, false);427 actual = targetService.selectAdapter(true, false);428 });429 Assert.Equal(expected, actual);430 }431 [Fact]432 function PersistentNotRespectedIfNotPossible() {433 var expected = "PersistentFalseSecureTrue";434 var actual;435 mockA(function() {436 var targetService = new Aura.Services.AuraStorageService();437 registerAdapter(targetService, expected, false, true);438 actual = targetService.selectAdapter(true, false);439 });440 Assert.Equal(expected, actual);441 }442 }443 [Fixture]444 function deleteStorage() {445 var mockA = Mocks.GetMocks(Object.Global(), {446 "Aura": {447 "Storage": {448 "MemoryAdapter": {449 "NAME": "memory"450 }451 }452 },453 "$A": {454 assert: function(condition, assertMessage) {455 if (!condition) { throw new Error(assertMessage); }456 },457 util: {458 isString: function(obj) { return typeof obj === "string"; },459 isFunction: function(obj) { return true; }460 }461 },462 });463 [Fact]464 function DeleteCallsAdapterDelete() {465 var name = "arbitrary";466 var actual;467 mockA(function() {468 var targetService = new Aura.Services.AuraStorageService();469 targetService.storages[name] = {470 deleteStorage: function() {471 actual = true;472 }473 };474 targetService.deleteStorage(name);475 });476 Assert.True(actual);477 }478 [Fact]479 function DeleteCausesGetStorageToReturnNull() {480 var name = "arbitrary";481 var actual;482 mockA(function() {483 var targetService = new Aura.Services.AuraStorageService();484 targetService.storages[name] = {485 deleteStorage: function() {}486 };487 targetService.deleteStorage(name);488 actual = targetService.getStorage(name);489 });490 Assert.Undefined(actual);491 }492 [Fact]493 function DeleteNonExistentStorage() {494 var name = "arbitrary";495 var actual496 mockA(function() {497 var targetService = new Aura.Services.AuraStorageService();498 targetService.deleteStorage(name);499 actual = targetService.getStorage(name);500 });501 Assert.Undefined(actual);502 }503 }504 [Fixture]505 function getStorage() {506 var mockA = Mocks.GetMocks(Object.Global(), {507 "$A": {508 util: {509 isString: function(obj){510 return typeof obj === 'string';511 }512 },513 assert: function(condition, message){514 if (!condition) {515 throw new Error(message);516 }517 }518 }519 });520 [Fact]521 function ThrowsErrorWhenCalledWithNonString() {522 var expected = "AuraStorageService.getStorage(): 'name' must be a String.";523 var actual;524 mockA(function() {525 var targetService = new Aura.Services.AuraStorageService();526 try {527 targetService.getStorage(null);528 } catch (e) {529 actual = e.toString();530 }531 });532 Assert.Equal(expected, actual);533 }534 [Fact]535 function ReturnsUndefinedWhenStorageNotExists() {536 var actual;537 mockA(function() {538 var targetService = new Aura.Services.AuraStorageService();539 actual = targetService.getStorage("NotExists");540 });541 Assert.Undefined(actual);542 }543 [Fact]544 function ReturnsStorageWhenStorageExists() {545 var storageName = "storageName";546 var expected = {};547 var actual;548 mockA(function() {549 var targetService = new Aura.Services.AuraStorageService();550 targetService.storages[storageName] = expected;551 actual = targetService.getStorage(storageName);552 });553 Assert.Equal(expected, actual, "Failed to return corresponding storage.");554 }555 }...

Full Screen

Full Screen

poly-tg82-device-service.test.js

Source:poly-tg82-device-service.test.js Github

copy

Full Screen

1const PolyTg82DeviceService = require('../poly-tg82-device-service')2describe('poly-tg82-device-service', () => {3 const upCommandBytes = new Uint8Array([10, 2, 1, 1, 18, 2, 1, 2])4 const downCommandBytes = new Uint8Array([10, 2, 2, 2, 18, 2, 1, 2])5 const getVersionReturn =6 '<tg82><my><version><hw>0</hw><app>2.92</app><xml>1.18 / 1.84 / 1.04</xml><release>006.102_20210223</release><os>1.32</os><logic>20</logic><web>1.99</web><webapp>1.66</webapp><radio_conf>2.16</radio_conf><xml_disp>2.02</xml_disp><scoring>4.18</scoring><rotary_1>-.--</rotary_1><rotary_2>-.--</rotary_2></version></my></tg82>'7 it('exists', async () => {8 const targetService = new PolyTg82DeviceService()9 expect(targetService).toBeDefined()10 expect(Object.keys(targetService.rabbitMqInstance).length).toBe(0)11 expect(Object.keys(targetService.tcpConnections).length).toBe(4)12 expect(Object.keys(targetService.tcpConnections.connections).length).toBe(0)13 expect(Object.keys(targetService.tcpConnections.status).length).toBe(0)14 expect(targetService.deviceIpMap).toBeDefined()15 expect(targetService.deviceCommands).toBeDefined()16 expect(targetService.onDataReceive).toBeDefined()17 expect(targetService.debugPublishMessage).toBeDefined()18 })19 it('creates', async () => {20 const targetService = await PolyTg82DeviceService.create({21 debug: false,22 test: true23 })24 expect(targetService.rabbitMqInstance.connected).toBeDefined()25 })26 it('Parses valid message', async () => {27 const targetService = await PolyTg82DeviceService.create({28 debug: false,29 test: true30 })31 targetService.onDataReceive(upCommandBytes)32 expect(targetService.data.transmits).toBe(6)33 })34 it('Rejects invalid message', async () => {35 const targetService = await PolyTg82DeviceService.create({36 debug: false,37 test: true38 })39 const commandBytes = new Uint8Array([1, 1, 1, 1, 1, 1, 1, 1])40 expect(targetService.onDataReceive(commandBytes)).toBe(false)41 expect(targetService.data.transmits).toBe(4)42 })43 it('Filters single bad ID', async () => {44 const targetService = await PolyTg82DeviceService.create({45 debug: false,46 test: true47 })48 const commandBytes = new Uint8Array([10, 2, 1, 1, 18, 2, 1, 8]) // Invalid ID = 8, only 4 registered in demo code49 expect(targetService.onDataReceive(commandBytes)).toBe(true)50 expect(targetService.data.transmits).toBe(5)51 expect(targetService.tcpConnections.lastRecvMessage[1]).toBe(52 targetService.deviceCommands[1].command53 )54 expect(targetService.tcpConnections.lastRecvMessage[2]).toBe(55 getVersionReturn56 )57 })58 it('Filters multiple bad ID', async () => {59 const targetService = await PolyTg82DeviceService.create({60 debug: false,61 test: true62 })63 const commandBytes = new Uint8Array([10, 2, 1, 1, 18, 2, 7, 8]) // 7 + 8 are invalid Ids64 expect(targetService.onDataReceive(commandBytes)).toBe(false)65 expect(targetService.data.transmits).toBe(4)66 expect(targetService.tcpConnections.lastRecvMessage[1]).toBe(67 getVersionReturn68 )69 expect(targetService.tcpConnections.lastRecvMessage[2]).toBe(70 getVersionReturn71 )72 })73 it('Filters single bad command', async () => {74 const targetService = await PolyTg82DeviceService.create({75 debug: false,76 test: true77 })78 const commandBytes = new Uint8Array([10, 2, 1, 100, 18, 2, 1, 2]) // 7 + 8 are invalid Ids according to map containing 4 addresses79 expect(targetService.onDataReceive(commandBytes)).toBe(false)80 expect(targetService.data.transmits).toBe(5)81 expect(targetService.tcpConnections.lastRecvMessage[1]).toBe(82 targetService.deviceCommands[1].command83 )84 expect(targetService.tcpConnections.lastRecvMessage[2]).toBe(85 getVersionReturn86 )87 })88 it('Filters multiple bad command', async () => {89 const targetService = await PolyTg82DeviceService.create({90 debug: false,91 test: true92 })93 const commandBytes = new Uint8Array([10, 2, 200, 100, 18, 2, 1, 2]) // 100 Results in setting 'Unknown', but 200 results in undefined94 expect(targetService.onDataReceive(commandBytes)).toBe(false)95 expect(targetService.data.transmits).toBe(4)96 expect(targetService.tcpConnections.lastRecvMessage[1]).toBe(97 getVersionReturn98 )99 expect(targetService.tcpConnections.lastRecvMessage[2]).toBe(100 getVersionReturn101 )102 })103 it('Sends up XML command', async () => {104 const targetService = await PolyTg82DeviceService.create({105 debug: false,106 test: true107 })108 expect(targetService.onDataReceive(upCommandBytes)).toBe(true)109 expect(targetService.data.transmits).toBe(6)110 expect(targetService.tcpConnections.lastRecvMessage[1]).toBe(111 targetService.deviceCommands[1].command112 )113 expect(targetService.tcpConnections.lastRecvMessage[2]).toBe(114 targetService.deviceCommands[1].command115 )116 })117 it('Sends down XML command', async () => {118 const targetService = await PolyTg82DeviceService.create({119 debug: false,120 test: true121 })122 expect(targetService.onDataReceive(downCommandBytes)).toBe(true)123 expect(targetService.data.transmits).toBe(6)124 expect(targetService.tcpConnections.lastRecvMessage[1]).toBe(125 targetService.deviceCommands[2].command126 )127 expect(targetService.tcpConnections.lastRecvMessage[2]).toBe(128 targetService.deviceCommands[2].command129 )130 })131 it('Sends get version', async () => {132 const targetService = await PolyTg82DeviceService.create({133 debug: false,134 test: true135 })136 expect(targetService.tcpConnections.lastSendMessage[1]).toBe(137 targetService.getVersionCommand138 )139 expect(targetService.tcpConnections.lastSendMessage[2]).toBe(140 targetService.getVersionCommand141 )142 expect(targetService.tcpConnections.lastRecvMessage[1]).toBe(143 getVersionReturn144 )145 expect(targetService.tcpConnections.lastRecvMessage[2]).toBe(146 getVersionReturn147 )148 })...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1import { targetService } from 'ng-mocks';2import { MockBuilder } from 'ng-mocks';3import { MockRender } from 'ng-mocks';4import { MockModule } from 'ng-mocks';5import { MockProvider } from 'ng-mocks';6import { MockDirective } from 'ng-mocks';7import { MockComponent } from 'ng-mocks';8import { MockPipe } from 'ng-mocks';9import { MockInsta

Full Screen

Using AI Code Generation

copy

Full Screen

1import { targetService } from 'ng-mocks';2describe('MyService', () => {3 it('should be created', () => {4 const service: MyService = targetService(MyService);5 expect(service).toBeTruthy();6 });7});8import { MyService } from './my-service';9import { TestBed } from '@angular/core/testing';10import { targetService } from 'ng-mocks';11describe('MyService', () => {12 beforeEach(() => TestBed.configureTestingModule({}));13 it('should be created', () => {14 const service: MyService = targetService(MyService);15 expect(service).toBeTruthy();16 });17});18import { MyService } from './my-service';19import { targetService } from 'ng-mocks';20describe('MyService', () => {21 it('should be created', () => {22 const service: MyService = targetService(MyService);23 expect(service).toBeTruthy();24 });25});26import { MyService } from './my-service';27import { targetService } from 'ng-mocks';28describe('MyService', () => {29 it('should be created', () => {30 const service: MyService = targetService(MyService);31 expect(service).toBeTruthy();32 });33});34import { MyService } from './my-service';35import { targetService } from 'ng-mocks';36describe('MyService', () => {37 it('should be created', () => {38 const service: MyService = targetService(MyService);39 expect(service).toBeTruthy();40 });41});42import { MyService } from './my-service';43import { targetService } from 'ng-mocks';44describe('MyService', () => {45 it('should be created', () => {46 const service: MyService = targetService(MyService);47 expect(service).toBeTruthy();48 });49});50import { MyService } from './my-service';51import { targetService } from 'ng-mocks';52describe('MyService', () => {53 it('should be created', () => {54 const service: MyService = targetService(MyService);55 expect(service).toBeTruthy();56 });57});58import { MyService } from './my-service';59import { targetService }

Full Screen

Using AI Code Generation

copy

Full Screen

1import { targetService } from 'ng-mocks';2import { MyService } from './my.service';3import { TestBed } from '@angular/core/testing';4describe('MyService', () => {5 beforeEach(() => TestBed.configureTestingModule({}));6 it('should be created', () => {7 const service: MyService = targetService(MyService);8 expect(service).toBeTruthy();9 });10});11import { Injectable } from '@angular/core';12@Injectable({13})14export class MyService {15 constructor() { }16}17{18 "compilerOptions": {19 }20}21 { pattern: 'src/**/*.ts', watched: false }22preprocessors: {23},24 require('karma-jasmine'),25 require('karma-chrome-launcher'),26 require('karma-typescript'),27 require('karma-coverage'),28 require('karma-jasmine-html-reporter')29coverageReporter: {30 { type: 'text-summary' },31 { type: 'html' },32 { type: 'karma-typescript' }33},34karmaTypescriptConfig: {35 {36 }37}

Full Screen

Using AI Code Generation

copy

Full Screen

1import { targetService } from 'ng-mocks';2import { TestBed } from '@angular/core/testing';3import { Service } from './service';4import { ServiceModule } from './service.module';5describe('Service', () => {6 beforeEach(() => {7 TestBed.configureTestingModule({8 imports: [ServiceModule],9 });10 });11 it('should be created', () => {12 const service: Service = targetService(Service);13 expect(service).toBeTruthy();14 });15});16import { NgModule } from '@angular/core';17import { Service } from './service';18@NgModule({19})20export class ServiceModule {}21import { Injectable } from '@angular/core';22@Injectable()23export class Service {24 public getValue(): string {25 return 'value';26 }27}28import { TestBed } from '@angular/core/testing';29import { Service } from './service';30import { ServiceModule } from './service.module';31describe('Service', () => {32 beforeEach(() => {33 TestBed.configureTestingModule({34 imports: [ServiceModule],35 });36 });37 it('should be created', () => {38 const service: Service = TestBed.get(Service);39 expect(service).toBeTruthy();40 });41});42import { TestBed } from '@angular/core/testing';43import { Service } from './service';44import { ServiceModule } from './service.module';45describe('Service', () => {46 beforeEach(() => {47 TestBed.configureTestingModule({48 imports: [ServiceModule],49 });50 });51 it('should be created', () => {52 const service: Service = TestBed.get(Service);53 expect(service).toBeTruthy();54 });55});56import { TestBed } from '@angular/core/testing';57import { Service } from './service';58import { ServiceModule } from './service.module';59describe('Service', () => {60 beforeEach(() => {61 TestBed.configureTestingModule({62 imports: [ServiceModule],63 });64 });65 it('should be created', () => {66 const service: Service = TestBed.get(Service);67 expect(service).toBeTruthy();68 });69});70import { TestBed } from '@angular/core/testing';71import { Service } from './service';72import { ServiceModule } from './service.module';73describe('Service', () => {74 beforeEach(() => {75 TestBed.configureTestingModule({76 imports: [ServiceModule],

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run ng-mocks automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful