Ionic testing - asynchronous look for a change in ionViewDidLoad [closed] - ionic-framework

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have troubles trying to test the value of a variable that change inside the ionViewDidLoad method
welcomepage.ts
#Component({...})
export class WelcomePage {
test: number = 0;
constructor(){}
ionViewDidLoad(){
this.test = 1;
}
}
welcomepage.spec.ts
it('test should equal be equal to 1', () => {
fixture.detectChanges();
expect(component.test).toBe(1)
});
But this test is not succeeding. If I do the change inside the constructor function (this.test = 1) then it is working.
======== UPDATE ========
It seems that it is a problem with ionViewDidLoad function triggered by ionic, it is working as usual by running ionic but not in the test.

It appears that ionic life-cycle events (ionViewDidEnter, ionViewDidLoad, ...) are not called during testing, by calling it manually it works :
it('test should equal be equal to 1', () => {
component.ionViewDidLoad();
fixture.detectChanges();
expect(component.test).toBe(1)
});
But then how are we sure that ionic really call ionViewDidLoad ?

Your variable is not setting because you have done it in wrong way. Your variable name is test and you are setting value in variable type which is number. Please see the following snippet, hope it helps.
#Component({...})
export class WelcomePage {
test: number = 0;
constructor(){}
ionViewDidLoad(){
this.test = 1;
}
}

Related

How to persist state from a class into a separate SwiftUI view? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am having problems persisting a class's state into a SwiftUI struct's view.
I have a class that acts as a controller, defined in one file, and a SwiftUI view that is supposed to change according to properties in that controller.
I've defined these files as such:
ClockController.swift
class ClockController:ObservableObject {
#Binding var isAM:Bool
init(){
self.isAM = false
}
func toggleAMPM(){
self.isAM = !self.isAM
}
}
and TestUI.swift
struct TestUI:View{
#ObservedObject var clockController:ClockController = ClockController()
var body: some View {
Button(action: {
self.clockController.toggleAMPM()
}){
Text("Toggle")
}
Text(self.clockController.isAM ? "AM" : "PM")
}
}
I want the TestUI to change/re-render every time the self.clockController.isAM variable changes (when the toggle button is pressed), which is why I have made ClockController an ObservableObject and added the #Binding keyword to the isAM property. However, I keep getting the following errors with this setup on ClockController's initializer method:
'self' used in property access 'isAM' before all stored properties are initialized and Return from initializer without initializing all stored properties
How can I get my TestUI to bind on ClockController's isAM variable?
all! I've fixed the problem myself. I needed to remove the #Binding keyword from ClockController's isAM property and change it to #Published. That did the trick.
Kudo's to YouTube and this video: https://youtu.be/-yjKAb0Pj60?t=919

Flutter : Voice command to give user weather and date time update [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 1 year ago.
Improve this question
I want to create an application that work on voice command. In this application there is two options when user ask for weather or time update the application get the current weather and give update about weather in voice and the same functionality for time and date. Please help me how I can achieve this in flutter.
You can either use :
https://alan.app/docs/tutorials/flutter/integrating-flutter
or use:
https://pub.dev/packages/speech_to_text
import 'package:speech_to_text/speech_to_text.dart' as stt;
stt.SpeechToText speech = stt.SpeechToText();
bool available = await speech.initialize( onStatus: statusListener, onError: errorListener );
if ( available ) {
speech.listen( onResult: resultListener );
}
else {
print("The user has denied the use of speech recognition.");
}
// some time later...
speech.stop()
Then treat specific words as commands:
void resultListener(SpeechRecognitionResult result) {
++resultListened;
print('Result listener $resultListened');
setState(() {
lastWords = '${result.recognizedWords} - ${result.finalResult}';
});
}

How to get Recent Photo? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Currently i and doing on an Flutter Camera Project. May i know how can i get the recently photo to show on the container like Instagram stories left bottom?
var result = await PhotoManager.requestPermission();
if (result) {
// success
List<AssetPathEntity> list = await PhotoManager.getAssetPathList(type: RequestType.image);
final assetList = await list[0].getAssetListRange(start: 0, end: 1);
filename = await assetList[0].file;
setState(() {
});
} else {
// fail
/// if result is fail, you can call `PhotoManager.openSetting();` to open android/ios applicaton's setting to get permission
}
This works for me for getting the most recent image from the device as a thumbnail according to the plugin used from the website given by you.

Sending mail by button [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I want to send e-mail after confirmation box. I make a button and push after i should choice yes or no. If NO nothing do. If yes i want to start to SendAnEmail.
Please help me. How can I start the function SendAnEmail?
Thank you.
function SendAnEmail() {
var email = 'my#mail.com'
var subject = 'mySubject';
var body = 'body of mail';
GmailApp.sendEmail(email, subject, body);
}
function test(){
var ui = SpreadsheetApp.getUi();
var ret = ui.alert('Do you want send e-mail?', ui.ButtonSet.YES_NO);
if(ret == ui.Button.YES)
{GmailApp.sendEmail}
}
Thank you
You are almost there
The only thing you need to do is to call function SendAnEmail() when the button Yes is pressed.
So:
function test(){
var ui = SpreadsheetApp.getUi();
var ret = ui.alert('Do you want send e-mail?', ui.ButtonSet.YES_NO);
if(ret == ui.Button.YES){
SendAnEmail()
}
}

jQuery Combobox/select autocomplete? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
Does a jQuery plug-in exist for replacing select/combo box?
I tried SexyCombo, and it is as close to what I want, but it doesn't complete if you are writing from middle, only from beginning.
I have 2 levels of categories (20 top level categories, and with subcategories in total 120 categories), so when user is submitting an entry, he must find desired category as soon as possible.
So... 2 levels + autocomplete populate text even if you write middle letters.
Or any other solution?
Have a look at the following example of the jQueryUI Autocomplete, as it is keeping a select around and I think that is what you are looking for. Hope this helps.
http://jqueryui.com/demos/autocomplete/#combobox
[edit] The lovely chosen jQuery plugin has been bought to my attention, looks like a great alternative to me.
Or if you just want to use jQuery autocomplete, I've extended the combobox example to support defaults and remove the tooltips to give what I think is more expected behaviour. Try it out.
(function ($) {
$.widget("ui.combobox", {
_create: function () {
var input,
that = this,
wasOpen = false,
select = this.element.hide(),
selected = select.children(":selected"),
defaultValue = selected.text() || "",
wrapper = this.wrapper = $("<span>")
.addClass("ui-combobox")
.insertAfter(select);
function removeIfInvalid(element) {
var value = $(element).val(),
matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex(value) + "$", "i"),
valid = false;
select.children("option").each(function () {
if ($(this).text().match(matcher)) {
this.selected = valid = true;
return false;
}
});
if (!valid) {
// remove invalid value, as it didn't match anything
$(element).val(defaultValue);
select.val(defaultValue);
input.data("ui-autocomplete").term = "";
}
}
input = $("<input>")
.appendTo(wrapper)
.val(defaultValue)
.attr("title", "")
.addClass("ui-state-default ui-combobox-input")
.width(select.width())
.autocomplete({
delay: 0,
minLength: 0,
autoFocus: true,
source: function (request, response) {
var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
response(select.children("option").map(function () {
var text = $(this).text();
if (this.value && (!request.term || matcher.test(text)))
return {
label: text.replace(
new RegExp(
"(?![^&;]+;)(?!<[^<>]*)(" +
$.ui.autocomplete.escapeRegex(request.term) +
")(?![^<>]*>)(?![^&;]+;)", "gi"
), "<strong>$1</strong>"),
value: text,
option: this
};
}));
},
select: function (event, ui) {
ui.item.option.selected = true;
that._trigger("selected", event, {
item: ui.item.option
});
},
change: function (event, ui) {
if (!ui.item) {
removeIfInvalid(this);
}
}
})
.addClass("ui-widget ui-widget-content ui-corner-left");
input.data("ui-autocomplete")._renderItem = function (ul, item) {
return $("<li>")
.append("<a>" + item.label + "</a>")
.appendTo(ul);
};
$("<a>")
.attr("tabIndex", -1)
.appendTo(wrapper)
.button({
icons: {
primary: "ui-icon-triangle-1-s"
},
text: false
})
.removeClass("ui-corner-all")
.addClass("ui-corner-right ui-combobox-toggle")
.mousedown(function () {
wasOpen = input.autocomplete("widget").is(":visible");
})
.click(function () {
input.focus();
// close if already visible
if (wasOpen) {
return;
}
// pass empty string as value to search for, displaying all results
input.autocomplete("search", "");
});
},
_destroy: function () {
this.wrapper.remove();
this.element.show();
}
});
})(jQuery);
I know this has been said earlier, but jQuery Autocomplete will do exactly what you need. You should check out the docs as the autocomplete is very customizable. If you are familiar with javascript then you should be able to work this out. If not I can give you a few pointers, as I have done this once before, but beware I am not well versed in javascript myself either, so bear with me on this.
I think the first thing you should do is just get a simple autocomplete text field working on your page, and then you can customize it from there.
The autocomplete widget accepts JSON data as it's 'source:' option. So you should set-up your app to produce the 20 top level categories, and subcategories in JSON format.
The next thing to know is that when the user types into your textfield, the autocomplete widget will send the typed values in a parameter called "term".
So let's say you first set-up your site to deliver the JSON data from a URL like this:
/categories.json
Then your autocomplete source: option would be 'source: /categories.json'.
When a user types into the textfield, such as 'first-cata...' the autocomplete widget will start sending the value in the 'term' parameter like this:
/categories.json?term=first-cata
This will return JSON data back to the widget filtered by anything that matches 'first-cata', and this is displayed as an autocomplete suggestion.
I am not sure what you are programming in, but you can specify how the 'term' parameter finds a match. So you can customize this, so that the term finds a match in the middle of a word if you want. Example, if the user types 'or' you code could make a match on 'sports'.
Lastly, you made a comment that you want to be able to select a category name but have the autocomplete widget submit the category ID not the name.
This can easily be done with a hidden field. This is what is shown in the jQuery autocomplete docs.
When a user selects a category, your JavaScript should update a hidden field with the ID.
I know this answer is not very detailed, but that is mainly because I am not sure what you are programming in, but the above should point you in the right direction. The thing to know is that you can do practically any customizing you want with this widget, if you are willing to spend the time to learn it.
These are the broad strokes, but you can look here for some notes I made when I implemented something similar to what you want in a Rails app.
Hope this helped.
This works great for me and I'm doing more, writing less with jQuery's example modified.
I defined the select object on my page, just like the jQuery ex. I took the text and pushed it to an array. Then I use the array as my source to my input autocomplete. tadaa.
$(function() {
var mySource = [];
$("#mySelect").children("option").map(function() {
mySource.push($(this).text());
});
$("#myInput").autocomplete({
source: mySource,
minLength: 3
});
}
jQuery 1.8.1 has an example of this under autocomplete. It's very easy to implement.