Jest mockResolvedValue missing axios properties - axios

If Jest's mockResolvedValue return type is AxiosResponse, why are most of properties you'd expect in a AxiosResponse missing?
Example:
create a spy to spy on some API
const spy = jest.spyOn(someAPI, "makeRequest")
Then set up a mock response..
spy.mockResolvedValue({ data: successRes })
The return type of mockResolvedValue is...
jest.SpyInstance<Promise<AxiosResponse<unknown>>.
Yes the following Typescript error occurs:
...is missing the following properties from type 'AxiosResponse<unknown>': status, statusText, headers, config

Related

Argument of type 'typeof Row' is not assignable to parameter of type 'ConstructorOfATypedSvelteComponent

I'm using Svelte in combination with a component library named svelte-materialify. For all the components of this library VSCode gives me the following error message when I import one of the components into my pages, e.g. when I import the 'Row' component:
Argument of type 'typeof Row' is not assignable to parameter of type 'ConstructorOfATypedSvelteComponent'.
Types of construct signatures are incompatible.
Type 'new (props: RowProps & { children?: any; class?: string; } & Record<string, any>) => Row' is not assignable to type
'new (args: { target: any; props?: any; }) => ATypedSvelteComponent'.
Type 'SvelteComponent<RowProps>' is missing the following properties from type 'ATypedSvelteComponent': $$events_def, $$slot_def
Possible causes:
- You use the instance type of a component where you should use the constructor type
- Type definitions are missing for this Svelte Component. If you are using Svelte 3.31+, use SvelteComponentTyped to add a definition:
import type { SvelteComponentTyped } from "svelte";
The applications compiles and runs fine without any error messages in the browser. I have no clue what this error tries to tell me and how I can fix it. I would be very thankful if somebody could point me into the right direction.

Flutter: http response based on model strict type

I want to implement a strict type http request/response. I'm new in flutter but have some experience in typescript. I don't know if I'm doing it right. Sorry I dont have enough reputation to post the screenshot of my code.
Actual screenshot of code
My first issue is on line 21: "type 'String' is not a subtype of type 'Api' in type cast". I want to convert the response.body to the same expected response of the login func
Then next is line 31, my concern here is the error variable is a dynamic type that cannot be 'Api' response. Can I make it possible to have to expected response type? or any workaround to this?
I hope someone could help me with this one. Thank you in advance.
The body is a json, you need to parse it first and create the Api model from it.
import 'dart:convert';
...
var responseData = jsonDecode(restResponse.data); //
if(responseData is Map){
Data.fromJson(responseData); //you can read the map entries and create a Data entry.
}
You can write a base class as response, and have a success class and an error class, and you can check those from the caller side. I'd suggest to return the parsed json without any model mapping and map the json to model from the caller side.
abstract class ResponseBase {}
class ResponseSuccess extends ResponseBase {
final dynamic data;
ResponseSuccess(this.data);
}
class ResponseError extends ResponseBase {
final int statusCode;
ResponseError(this.statusCode);
}
Future<ResponseBase> login(...
And from the caller:
whenLoginClicked() async{
var response = await repository.login(data, devideId);
if(response is ResponseSuccess){
// parse the response.data
} else if(response is ResponseError){
// handle error
}
}

dart:js error when calling promiseToFuture - NoSuchMethodError: tried to call a non-function, such as null: 'jsPromise.then'

I'm trying to await a custom global JavaScript function:
var promise = js.context.callMethod('performAuthenticationInNewWindow', [uri.toString()]);
print(promise);
var qs = await promiseToFuture(promise);
Which prints the following:
[object Promise]
NoSuchMethodError: tried to call a non-function, such as null: 'jsPromise.then'
I came across the same mistake. Given the name dart:js_util of the package which contains the function promiseToFuture(), I too thought the function should be used with a object obtained with dart:js, but is not so, and the sample in the doc is actually very clear.
The javascript Promise object must be obtained using the #JS() annotation of package:js.
Example:
#JS()
library my_lib; //Not avoid the library annotation
import 'dart:js_util';
import 'package:js/js.dart';
#JS()
external performAuthenticationInNewWindow(String uri);
performAuth(uri) async {
var promise = performAuthenticationInNewWindow(uri.toString());
var qs = await promiseToFuture(promise);
print(qs);
}
Note to avoid mistake:
if a function for the interop with Javascript require an object obtained with the dart:js package, the declared type is usally not Object but JsObject or subclasses. Instead, if the object must be obtained using the #JS annotation, the declared type is Object, if it doesn't exists an appropriate external declaration
(the runtimeType of the objects obtained with the #JS annotation is NativeJavaScriptObject, but there is no corresponding class exposed in the Dart Sdk).

broccoli-caching-writer fails with "Cannot read property 'images/.DS_Store' of undefined"

I am fairly new to broccoli and have been trying to write a simple plugin to use with ember.js. I used broccoli-caching-writer in my index.js as described on the github page for broccoli-caching-writer:
var CachingWriter = require('broccoli-caching-writer');
module.exports = CachingWriter.extend({
init: function(inputTrees, options)
{
console.log('Initializing plugin with tree');
console.log(inputTrees);
console.log(options);
this.inputTrees = inputTrees;
},
updateCache: function(srcPaths, destDir) {
console.log('updateCache called with srcPaths and destDir');
console.log(srcPaths);
console.log(destDir);
}
});
I then imported the plugin into my ember app (that uses ember CLI) and configured the following in my .brocfile
var plugin = require('broccoli-my-plugin');
var merge = require('broccoli-merge-trees');
pluginTree = new svgSpriter(['images'], {some: 'options'});
....
....
module.exports = merge([app.toTree(),pluginTree]);
Running the above with ember build command gives the following output (paths edited for privacy reasons):
Build failed.
Cannot read property 'images/.DS_Store' of undefined
TypeError: Cannot read property 'images/.DS_Store' of undefined
at CoreObject.proto.shouldBeIgnored (/node_modules/broccoli-svg-sprite/node_modules/broccoli-caching-writer/index.js:135:33)
at CoreObject.proto.keyForTree (/node_modules/broccoli-svg-sprite/node_modules/broccoli-caching-writer/index.js:277:14)
at CoreObject.<anonymous> (/node_modules/broccoli-caching-writer/index.js:267:21)
at Array.map (native)
at CoreObject.proto.keyForTree (/node_modules/broccoli-caching-writer/index.js:266:24)
at /node_modules/broccoli-caching-writer/index.js:87:20
at lib$rsvp$$internal$$tryCatch (/node_modules/broccoli-svg-sprite/node_modules/broccoli-caching-writer/node_modules/rsvp/dist/rsvp.js:489:16)
at lib$rsvp$$internal$$invokeCallback (/node_modules/broccoli-svg-sprite/node_modules/broccoli-caching-writer/node_modules/rsvp/dist/rsvp.js:501:17)
at lib$rsvp$$internal$$publish (/node_modules/broccoli-svg-sprite/node_modules/broccoli-caching-writer/node_modules/rsvp/dist/rsvp.js:472:11)
at lib$rsvp$asap$$flush (/node_modules/broccoli-caching-writer/node_modules/rsvp/dist/rsvp.js:1290:9)
It seems like the plugin is trying to check whether to ignore the path or not, but the options passed to caching writer does not have filterfromcache option defined, so the above should work? Not sure if I'm missing something?
Any help would be appreciated.
Okay, I think the example for overriding the init method needs a little update for newbies like me.
It turns out that the init method in the parent module wasn't being called. Adding the following to my init method fixed it:
CachingWriter.prototype.init.call(this, inputTrees, options);
Not sure if there is a better way though.

Access Store From Initializer in ember-cli

I have an initializer and the following is coming back as undefined:
container.lookup('store:main')
I am trying to implement this: http://say26.com/using-rails-devise-with-ember-js
Has anybody any ideas why this is?
Here is a code snippet on how I was able to inject the store into all components.
//initializers/general-setup.js
import Ember from 'ember';
//The intent of this file is to hold initializers
//that are general and lack specific context or scope
export default {
name: 'general-setup',
after: 'store',
initialize: function(container) {
container.injection('component', 'store', 'store:main');
}
};