Property 'length' does not exist on type 'string'? - typescript2.0

Looking at some code in VS and this shows a red line under 'length', with a message in the title. Using typescript 2.2.2
What is going on?
addFilterTag = (value:string, type: Enums.FilterType): void =>
{
if (value.length > 0)
{
}
}

Related

Cypress conditional statement depending on whether the input field is disabled or not

I have some input fields on a page. They can be disabled or not. So I need to type the text in this field just in case when it's not disabled. I try to do this way:
fillPickUpTown(pickUpTown: string) {
cy.get(`[data-testid="pick-up-town"]`)
.should('not.be.disabled')
.then(() => {
cy.get(`[data-testid="pick-up-town"]`).type(pickUpTown);
});
}
But I have failed test with error "Timed out retrying after 10000ms: expected '' not to be 'disabled'".
How can I type to this field just when it's not disabled and do nothing when it is?
Drop the should, this will only succeed for the one condition. Instead, test inside then()
fillPickUpTown(pickUpTown: string) {
cy.get(`[data-testid="pick-up-town"]`)
.then($el => {
if (!$el.is(':disabled')) {
cy.wrap($el).type(pickUpTown);
}
});
}
You can use JQuery :enabled to check and implement If-Else.
fillPickUpTown(pickUpTown: string) {
cy.get(`[data-testid="pick-up-town"]`).then(($ele) => {
if ($ele.is(":enabled")) {
cy.get(`[data-testid="pick-up-town"]`).type(pickUpTown)
}
})
}

chai throw with property deep equal

expect.to.throw returns a Proxy to the thrown Error, so I can use with.property in order to check some properties of the error.
I attach a details object on my custom errors but I can't test for them, since the with.property compares only using strict equals.
Can I compare this property using deep equal somehow?
Example:
class DocNotFoundError extends Error {
constructor(message, docId) {
super(message)
this.details = { docId }
}
}
const getDoc = id => {
throw new DocNotFoundError('errors.docNotFound', id)
}
const docNotFound = expect(() => getDoc('01234567890')).to.throw('DocNotFoundError')
docNotFound.with.property('details', { docId: '01234567890' }) // fails
The error will fail similar to
AssertionError: expected { Object (error, ...) } to have property 'details' of { Object (docId) }, but got { Object (docId) }
+ expected - actual
I assume this is due to it only checks for reference equality and not deep object equality.
First of all there is a typo in the question: DocNotFoundError should not be in quotes.
I managed to get it working with docNotFound.to.have.deep.property('details', { docId: '01234567890' }), so yes you should perform deep comparison to check if objects have keys with same values.
Source 1
Source 2

Validating Undefined in React Native

I have a situation where I'm trying to pass some params via onPress to another screen. However I'm unable to capture the Undefined state and it gives me the red screen error -
Type Error: undefined is not an object.
Here's my snippet. Am I missing something?
const pressHandle = (item) => {
const capVal = state.payload.find((grabVal) => grabVal.jobId === item.jobId).jobId;
console.log(capVal); // in a defined case, I have a log so the query is correct.
if( typeof(capVal) !== undefined || typeof(capVal) != null ) {
setGrabber(capVal);
} else {
setGrabber('undefined!');
}
console.log(grabber);
}
You just have to do a null check your error is probably appearing due to accessing a property of an undefined item when finding an item.
Adding ?.jobId will access the jobId only if an item is found and then you can check if(capVal) which will run if capVal has a value
const pressHandle = (item) => {
const capVal = state.payload.find((grabVal) => grabVal.jobId === item.jobId)?.jobId;
console.log(capVal); // in a defined case, I have a log so the query is correct.
if(capVal) {
setGrabber(capVal);
} else {
setGrabber('undefined!');
}
console.log(grabber);
}

dojo on.js TypeError matchesTarget is undefined

I'm working to extend some legacy dojo code (v1.8). I added a button which when clicked calls a simple handle function. The problem is, nothing happens when I click the button and I get the following error in Firebug:
TypeError: matchesTarget is undefined
Everthing worked before, and I only added the following code:
require(["dojo/on"], function (on) {
on(document.getElementById("submitBtn"), "button:click", function (e) {
onSubmitQuery();
});
});
onSubmitQuery:function () {
var model_type_uuid = document.getElementById("modelTypeSelect").get('value');
// check to see if model_type_uuid is not undefined before submitting
if (model_type_uuid === undefined || model_type_uuid == "00000000-0000-0000-0000-000000000000") {
alert('Invalid Decision Model Type ' + model_type_uuid + ' for Decision Query submission');
return;
}
if (document.getElementByID("modeSelector").get('value') == "simulate") {
submitStandingQuery(model_type_uuid);
} else {
submitInteractiveQuery(model_type_uuid);
}
}
I've been pulling my hair out trying to figure this out. Please help!
You need to add the dojo/query module in order to match the selector button within its parent node submitBtn.
require(["dojo/on", "dojo/query"], function (on) {
on(document.getElementById("submitBtn"), "button:click", function (e) {
onSubmitQuery();
});
});

breezejs addEntityType issue

I'm new to breezejs. I am trying to define my entity type in the client without getting metadata from the server. I have a property called ID in the server entity.
I've defaulted the naming convention in the client side to camel case using the following code.
breeze.NamingConvention.camelCase.setAsDefault();
so, I started to map the entity as follows
store.addEntityType({
shortName: "Photo",
namespace: "MyProj.Models",
dataProperties: {
id: {
dataType: DataType.Guid,
isNullable: false,
isPartOfKey: true
},
title: {
dataType: DataType.String
},
description: {
dataType: DataType.String
},
createdDate: {
dataType: DataType.DateTime
},
}
});
This worked all fine, except the id field is not getting the proper value. instead, it has the default value set by the breeze datatype ctor which is equals to Guid.Empty.
by stepping through breezejs debug script, I found out that it looks for a property name called Id in the data that comes from the ajax request. But it can't find it as the property is ID so it initialize it to empty guid string. I assumed that by setting nameOnServer property of the dataProperty id, I will be able to fix it.
store.addEntityType({
shortName: "Photo",
namespace: "MyProj.Models",
dataProperties: {
id: {
dataType: DataType.Guid,
isNullable: false,
nameOnServer: 'ID',
isPartOfKey: true
},
title: {
dataType: DataType.String
},
description: {
dataType: DataType.String
},
createdDate: {
dataType: DataType.DateTime
},
}
});
But it didn't work.
Further digging through the breez.debug.js code, in the method updateClientServerNames on line 7154, it seems it ignores the nameOnServer that I have defined.
Am I missing something here?
Okay, Feel like I spent my whole life through breeze documentation. Anyways, Finally solved the issue. To be honest, this wasn't a problem in breeze (but I wonder why it doesn't override the actual nameOnServer when I provide one). It's an error made by one of the developers in the early stage of the database implementation (probably 6 years ago). If the database adhered to Pascal Case naming convention, things would have worked perfectly fine.
As a solution I wrote a custom naming convention which corrects the naming convention error when it has ID in the name and combines it with camelCase naming convention.
var createInconsistenIDConvention = function () {
var serverPropertyNameToClient = function (serverPropertyName, prop) {
if (prop && prop.isDataProperty && (prop.nameOnServer && prop.nameOnServer === "ID")) {
return "id";
} else {
var firstSection = serverPropertyName.substr(0, 1).toLowerCase();
var idSection = "";
if (serverPropertyName.substr(1).indexOf("ID") != -1) {
firstSection += serverPropertyName.substr(1, serverPropertyName.substr(1).indexOf("ID")).toLowerCase() + "Id";
} else {
firstSection += serverPropertyName.substr(1);
}
return firstSection;
}
}
var clientPropertyNameToServer = function (clientPropertyName, prop) {
if (prop && prop.isDataProperty && (prop.nameOnServer && prop.nameOnServer.indexOf("ID") != -1)) {
return prop.nameOnServer;
} else {
return clientPropertyName.substr(0, 1).toUpperCase() + clientPropertyName.substr(1);
}
}
return new breeze.NamingConvention({
name: "inconsistenID",
serverPropertyNameToClient: serverPropertyNameToClient,
clientPropertyNameToServer: clientPropertyNameToServer
});
};
Not sure if the way I've used nameOnServer property is not correct. I couldn't find any documentation on that in breeze website.
please note that the above code only consider situations like ID, CountryID, GameID, PersonID etc.
Problem solved for now.