Run custom JavaScript in Chromium with CodeceptJS? - codeceptjs

I need to mock the time for my CodeceptJS tests.
My React component uses the new Date() function:
const Component = () => {
console.log(new Date())
return <h1>Im a component</h1>
}
I need the component to think it's 2018. For my Jest unit tests this was straightforward:
import MockDate from 'mockdate';
MockDate.set('2018-10');
test("test something", ()=>{
// Actual test here
})
MockDate.reset();
How can I do the same with CodeceptJS? Ive tried using the date mocking module in the test:
Scenario('#test', async (CheckoutPage) => {
const MockDate = require('mockdate');
MockDate.set('2018-10');
// Actual test here
});
I also tried dependancy injection. The code within FIX-DATE monkey patches the date:
Scenario(
'#test',
(CheckoutPage, FixDate) => {
FixDate();
CheckoutPage.load();
pause();
}
).injectDependencies({ FixDate: require('./FIX-DATE') });
Neither of these have any affect on the date.

The issue is that CodeceptJS is running inside the browser, so you need to override date object of the browser.
Basically you need to override the Date Object of the browser, or the function that is used, for Example:
// create a date object for this Friday:
var d = new Date(2018, 0, 20);
//override Date constructor so all newly constructed dates return this Friday
Date = function(){return d;};
var now = new Date()
console.log(now);
Date.now = function () { return d};
console.log(Date.now());
This is the way to do that in pure JS, the second step is to integrate into codeceptjs, and this can be done using I.executeScript
for Example:
I.executeScript(function () {
var d = new Date(2018, 0, 20);
Date = function(){return d;};
})
You can also create a custom step, for example, I.fakeDate(new Date(2018, 0, 20))
module.exports = function() {
return actor({
fakeDate: function(date) {
I.executeScript(function (fakeDate) {
var d = fakeDate;
window.__original_date = Date;
Date = function(){return d;};
}, date);
},
fakeDateRestore: function() {
I.executeScript(function () {
Date = window.__original_date;
});
}
});
}
Then you just Fake the date when you need, and restore it.
I.Click('beofre');
I.fakeDate(new Date(2018,10,01));
// The test code here
I.fakeDateRestore();
Hope this helps #:-)

Related

React native Reference child failed

const { width: WIDTH } = Dimensions.get('window');
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1;
var yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd;
}
if (mm < 10) {
mm = '0' + mm;
}
today = mm + dd + yyyy;
class Attendance extends Component {
state = {
image: null,
submit: false,
loading: false,
face: null,
confidence: 0,
class: '',
flag: ''
};
async componentDidMount() {
await Permissions.askAsync(Permissions.CAMERA);
const { currentUser } = firebase.auth();
firebase
.database()
.ref('users/')
.child(currentUser.uid)
.on('value', snap =>
this.setState({
face: snap.val().image,
class: snap.val().stream_sem
})
);
}
render(){
return(
<View style={styles.Conatiner}>
<Loader loading={this.state.loading} />
{this.renderContent()}
</View>
);
}
renderContent = () => {
firebase.database().ref('attendance')
.child(this.state.class)
.child(today)
.on("value", snap => {
this.setState({ flag: snap.val().flag});
});
if(this.state.flag === "0") {
//**something**
}
else {
//**something**
}
}
}
When i am trying to write this.state.class, it is showing reference child failed. Here is the error detail i am getting.
My firebase database Picture is here. Can you please tell where i am going wrong. I want to access "flag" part of the database. CSE8 should match with the users stream_sem
ThankYou in advance
When your renderContent is called first time, your state.class is empty string, remember that firebase snapshot will be loaded asynchronously. Also, do not attach listeners to firebase in a render function that is called by render method, you will end up having a lot of listeners.
Your logic should be something like:
Get user data from firebase
If data is not null, set state
After state is set, get attendance data from database
Set state appropriately
Always write rough algorithm before jumping state to dev (Ignore if you already knew that). Happy learning, Cheers!

AngularJS: Creating a directive for converting a string to a Date on the fly

Angular UI Bootstrap changed the way of what the datepicker expects as ng-model in some version after 1.13.0. Before it was fine to give it an ISO date string, now it wants a Date object.
I consume ISO date strings from my API though, so I have to
convert them into Date objects before giving it to the datepicker and
convert them back to an ISO date string when storing it.
In the past I used a directive like this:
function DateObjectDirective() {
const directive = {
restrict: "A",
require: ["ngModel"],
link(scope, element, attributes, controllers) {
const ngModel = controllers[0];
ngModel.$formatters.unshift(value => {
let output = null;
if(value) {
output = moment(value).toDate();
}
return output;
});
ngModel.$parsers.unshift(value => {
let output = null;
if(value) {
output = moment(value).format();
}
return output;
});
},
};
return directive;
}
This no longer works though, as the following error is reported:
this.activeDate.getFullYear is not a function
My guess is that the datepicker still uses the string as reference. Is there any other way I can convert before giving my data to the datepicker?
I found out that the directive I posted does indeed still work. The only problem was the order in which AngularJS evaluated the directives.
For example:
<input ng-model="someDateString" uib-datepicker-popup="yyyy-MM-dd" woo-date-object>
In my case, woo-date-object was always evaluated before uib-datepicker-popup. The result was that the datepicker has always pushed its own formatter on top of ngModel.$formatters, thus eliminating the possibility for me to intervene.
The solution is to give the own directive a higher priority. UI's datepicker doesn't have one set, so anything above 0 (which is the default) works:
{
restrict: "A",
require: "ngModel",
priority: 9999,
link(scope, element, attributes, ngModel) {
ngModel.$formatters.push(value => {
let output = new Date();
if(value) { output = moment(value).toDate(); }
return output;
});
ngModel.$parsers.push(value => {
let output = null;
if(value) { output = moment(value).format(); }
return output;
});
},
}

Set Umbraco Property Editor Input to jQueryUI Datepicker

I'm close but still can't quite get this to work.
I have a new custom property editor that is loading correctly and is doing almost everything expected until I try to set the text field to be a jQuery UI element.
As soon as I add a directive in Angular for setting it to call the jQuery UI datepicker function, I get the following error suggesting it hasn't loaded the jQueryUI script library correctly:
TypeError: Object [object Object] has no method 'datepicker'
Trouble is, I can't see where I should be adding it as the logical places (to my mind, at least) seem to make no difference. Here is the code in full:
function MultipleDatePickerController($scope, assetsService) {
//tell the assetsService to load the markdown.editor libs from the markdown editors
//plugin folder
//assetsService
// .load([
// "http://code.jquery.com/ui/1.10.4/jquery-ui.min.js"
// ])
// .then(function () {
// //this function will execute when all dependencies have loaded
// });
//load the seperat css for the editor to avoid it blocking our js loading
assetsService.loadCss("/css/jquery-ui.custom.min.css");
if (!$scope.model.value) {
$scope.model.value = [];
}
//add any fields that there isn't values for
//if ($scope.model.config.min > 0) {
if ($scope.model.value.length > 0) {
for (var i = 0; i < $scope.model.value.length; i++) {
if ((i + 1) > $scope.model.value.length) {
$scope.model.value.push({ value: "" });
}
}
}
$scope.add = function () {
//if ($scope.model.config.max <= 0 || $scope.model.value.length < $scope.model.config.max) {
if ($scope.model.value.length <= 52) {
$scope.model.value.push({ value: "" });
}
};
$scope.remove = function (index) {
var remainder = [];
for (var x = 0; x < $scope.model.value.length; x++) {
if (x !== index) {
remainder.push($scope.model.value[x]);
}
}
$scope.model.value = remainder;
};
}
var datePicker = angular.module("umbraco").controller("AcuIT.MultidateController", MultipleDatePickerController);
datePicker.directive('jqdatepicker', function () {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, element, attrs, ngModelCtrl) {
$(function () {
element.datepicker({
dateFormat: 'dd/mm/yy',
onSelect: function (date) {
scope.$apply(function () {
ngModelCtrl.$setViewValue(date);
});
}
});
});
}
}
});
I faced the same problem when adapting a jQuery Date Range Picker for my Date Range Picker package for Umbraco 7. It's frustrating! The problem (I think) is that Angular's ng-model listens for "input" changes to trigger events and so doesn't pick up on a jQuery triggered event.
The way around it I found was to force the input event of the element you wish to update to fire manually, using jQuery's .trigger() event.
For example, the date picker I was using had this code for when a date was changed:
updateInputText: function () {
if (this.element.is('input')) {
this.element.val(this.startDate.format(this.format) + this.separator + this.endDate.format(this.format));
}
},
I just adapted it to force an input trigger by adding this.element.trigger('input') to the code block, so it now reads:
updateInputText: function () {
if (this.element.is('input')) {
this.element.val(this.startDate.format(this.format) + this.separator + this.endDate.format(this.format));
this.element.trigger('input');
}
},
This forces Angular to "see" the change and then ng-model is updated. There may well be a more elegant way (as I'm an Angular newbie), but I know this worked for me.
Got it. This is probably a bit of a hack, but it's simple and effective so it's a win nonetheless.
The assetsService call is the key, where I've put code into the deferred .then statement to call jQueryUI's datepicker on any item that has the "jqdp" CSS class:
//tell the assetsService to load the markdown.editor libs from the markdown editors
//plugin folder
assetsService
.load([
"/App_Plugins/Multidate/jquery-ui.min.js"
])
.then(function () {
//this function will execute when all dependencies have loaded
$('.jqdp').datepicker({ dateFormat: 'dd/mm/yy' });
});
I've then gone and added that class to my view:
<input type="text" jqdatepicker name="item_{{$index}}" ng-model="item.value" class="jqdp" id="dp-{{model.alias}}-{{$index}}" />
Finally, I've added a directive to ensure that dynamically-added items also display a datepicker:
datePicker.directive('jqdatepicker', function () {
return function (scope, element, attrs) {
scope.$watch("jqdatepicker", function () {
try{
$(element).datepicker({ dateFormat: 'dd/mm/yy' });
}
catch(e)
{}
});
};
});
As I said, this is possibly a bit hacky but it achieves the right result and seems like a simple solution.

Can't select future dates in phonegap app after iOS 7 upgrade

I am using phone gap date picker plugin for iOS, which was working fine, but can't select future date after iOS7 upgrade .
This is my js code,
// Handling for iOS and Android
$('.appointmentTime').on('click', function(e) {
var currentField = $(this);
window.plugins.datePicker.show((function() {
var o = {
date: new Date(),
mode: 'time', // date or time or blank for both
allowOldDates: true
};
if (myConfig.deviceType === myConfig.deviceTypeEnum.IOS) {
o.allowFutureDates = true;
}
return o;
})(), function(returnDate) {
var selectedDate;
selectedDate = new Date(returnDate);
currentField.blur();
});
}

Adding event listener on 'Today' button in jquery-ui datapicker

I'm using the datepicker form jQuery-ui-1.8.16.
I have the following code:
Site.Calendar = function() {
// Set default setting for all calendars
jQuery.datepicker.setDefaults({
showOn : 'both',
buttonImageOnly : true,
buttonText: '',
changeMonth : true,
changeYear : true,
showOtherMonths : true,
selectOtherMonths : true,
showButtonPanel : true,
dateFormat : "D, d M, yy",
showAnim : "slideDown",
onSelect: Site.Calendar.customiseTodayButton
});
};
Site.Calendar.customiseTodayButton = function(dateText, inst) {
console.log("hello");
};
My customiseTodayButton function is only getting triggered when I select an actual date and NOT on the Today button.
Is there any way to override how the today button work's in the jQuery datepicker?
Thanks
I found the following posted here:
Today button in jQuery Datepicker doesn't work
jQuery.datepicker._gotoToday = function(id) {
var target = jQuery(id);
var inst = this._getInst(target[0]);
if (this._get(inst, 'gotoCurrent') && inst.currentDay) {
inst.selectedDay = inst.currentDay;
inst.drawMonth = inst.selectedMonth = inst.currentMonth;
inst.drawYear = inst.selectedYear = inst.currentYear;
}
else {
var date = new Date();
inst.selectedDay = date.getDate();
inst.drawMonth = inst.selectedMonth = date.getMonth();
inst.drawYear = inst.selectedYear = date.getFullYear();
this._setDateDatepicker(target, date);
this._selectDate(id, this._getDateDatepicker(target));
}
this._notifyChange(inst);
this._adjustDate(target);
}
It simply rewrites the goToToday method and adds two new lines:
this._setDateDatepicker(target, date);
this._selectDate(id, this._getDateDatepicker(target));
Maybe there is a cleaner way to fix this with your original answer Mark?
There isn't a standard event for when the today button is clicked. However, taking a look at the jquery.ui.datepicker.js code, it appears that it calls $.datepicker._gotoToday. I'll assume by customizeTodayButton you're attempting to change the behavior of what it does currently (not the looks, the looks would be done with styling). To change the existing behavior, it's good to know what it does now. So, that in mind, this is the current code of the function used:
/* Action for current link. */
_gotoToday: function(id) {
var target = $(id);
var inst = this._getInst(target[0]);
if (this._get(inst, 'gotoCurrent') && inst.currentDay) {
inst.selectedDay = inst.currentDay;
inst.drawMonth = inst.selectedMonth = inst.currentMonth;
inst.drawYear = inst.selectedYear = inst.currentYear;
}
else {
var date = new Date();
inst.selectedDay = date.getDate();
inst.drawMonth = inst.selectedMonth = date.getMonth();
inst.drawYear = inst.selectedYear = date.getFullYear();
}
this._notifyChange(inst);
this._adjustDate(target);
},
To override this function with your own functionality, you'll want to do update your code to something like this:
Site.Calendar = function() {
//override the existing _goToToday functionality
$.datepicker._gotoTodayOriginal = $.datepicker._gotoToday;
$.datepicker._gotoToday = function(id) {
// now, call the original handler
$.datepicker._gotoTodayOriginal.apply(this, [id]);
// invoke selectDate to select the current date and close datepicker.
$.datepicker._selectDate.apply(this, [id]);
};
// Set default setting for all calendars
jQuery.datepicker.setDefaults({
showOn: 'both',
buttonImageOnly: true,
buttonText: '',
changeMonth: true,
changeYear: true,
showOtherMonths: true,
selectOtherMonths: true,
showButtonPanel: true,
dateFormat: "D, d M, yy",
showAnim: "slideDown"
});
};
Also, here's a working jsFiddle of what you're looking for.
I realized the overriding of the today button in this way:
jQuery.datepicker._gotoToday = function(id) {
var today = new Date();
var dateRef = jQuery("<td><a>" + today.getDate() + "</a></td>");
this._selectDay(id, today.getMonth(), today.getFullYear(), dateRef);
};
This is quite simple and does the "select date and close datepicker" functionality that I would.

Resources