Comments (11)
thanks @skynet2 , I use karma/jasmine/phantomJS for all tests/mocks, no specific mock library
from ngx-google-places-autocomplete.
Hi @ddouras ,
Can you provide an example of your component`s test?
Do you use angular-cli ?
Thanks,
Stas
from ngx-google-places-autocomplete.
@skynet2 yes I use angular-cli
Here's a simple snippet of my spec file
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { GooglePlaceModule } from 'ngx-google-places-autocomplete';
describe('MyComponent component', () => {
let component: MyComponent;
let fixture: ComponentFixture<MyComponent>;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
GooglePlaceModule
],
declarations: [
MyComponent
],
schemas: [NO_ERRORS_SCHEMA],]
}).compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance;
});
it('Should create component', () => {
fixture.detectChanges();
expect(component).toBeTruthy();
});
});
I have also included below script to my index file. No sure if I am missing something. The screen works works perfectly fine with the auto complete, only issue I have is with karma.
<script src="https://maps.googleapis.com/maps/api/js?key={{MY_KEY}}&libraries=places&sensor=false&language=en"></script>
from ngx-google-places-autocomplete.
Hi @ddouras ,
I found pretty similar issue angular-ui/angular-google-maps#270
Looks like there few ways to resolve it, but the most suitable, I think will be to mock google object (last few messages from angular-ui discussion) https://github.com/ScottieR/angular-google-maps-mock
Thanks,
Stas
from ngx-google-places-autocomplete.
thanks @skynet2
I now get another error.
TypeError: undefined is not a function (evaluating 'this.autocomplete.addListener('place_changed', function () {
_this.handleChangeEvent();
})') in http://localhost:9876/_karma_webpack_/vendor.bundle.js (line 213559)
Do you think there is a way to mock the entire directive on my test?
from ngx-google-places-autocomplete.
Hi @ddouras ,
I added an additional check in a directive, can you try the latest version? 2.0.1
Not very familiar with karma, unfortunately.
Thanks,
Stas
from ngx-google-places-autocomplete.
Unfortunately, still same issue, I guess this needs to be addressed as Karma is very commonly used with Angular 4/5
from ngx-google-places-autocomplete.
Hi @ddouras ,
Yep, I`ll take a look at weekends.
Which mock lib you are using?
Thanks,
Stas
from ngx-google-places-autocomplete.
@ddouras @skynet2 I'm facing the same issue when testing with Karma/Jasmine in Angular 7
Error - ReferenceError: google is not defined
Is there any fix for this?
My Unit test
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { FormsModule } from '@angular/forms';
import { GooglePlaceModule } from "ngx-google-places-autocomplete";
import { RouterTestingModule } from '@angular/router/testing';
import { DefaultComponent } from './default.component';
describe('DefaultComponent', () => {
let component: DefaultComponent;
let fixture: ComponentFixture;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
FormsModule,
GooglePlaceModule,
RouterTestingModule
],
declarations: [DefaultComponent]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(DefaultComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
I have included my google places api script tag in index.html
Any help much appreciated.
from ngx-google-places-autocomplete.
Hey,
It worked for me this way -
All I did was followed the link https://github.com/ScottieR/angular-google-maps-mock
And copied the actual js content from 'https://maps.googleapis.com/maps/api/js?sensor=false'
and put into a file inside tests/mocks/gogole.js
In src/karma.config.js
files: [
'../tests/mocks/google.js'
]
That's it! Tests passed.
from ngx-google-places-autocomplete.
Hi @gitneeraj
I have tried your process but it's not working. Sometimes it's showing errors-
- Error: Google maps library cannot be found karma
- TypeError: google.maps.places.Autocomplete is not a constructor
Any help much appreciated.
====== spec.ts file =======
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { AddCompanyComponent } from './add-company.component';
import { SharedModule } from 'src/app/shared/shared.module';
import { HttpClientModule } from '@angular/common/http';
import { RouterTestingModule } from '@angular/router/testing';
import { ToastrModule } from 'ngx-toastr';
describe('AddCompanyComponent', () => {
let component: AddCompanyComponent;
let fixture: ComponentFixture;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [AddCompanyComponent],
imports: [SharedModule, HttpClientModule, RouterTestingModule, ToastrModule.forRoot()]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(AddCompanyComponent);
component = fixture.componentInstance;
fixture.detectChanges();
component.ngOnInit();
});
it('component should create', () => {
expect(component).toBeTruthy();
});
});
===== karma.config.js file =====
files: [
'app/service/mocks/google.js', // <- Only added this
],
from ngx-google-places-autocomplete.
Related Issues (20)
- Debounce time during type a text
- How can I set the HTML5 'autocomplete' value to something different to 'off'
- How TO change componentRestrictions -- Country Dynamically? HOT 4
- Testing Error With Angular 12
- Is project is still active ? what about angular 13 support HOT 1
- more than 1 input in the same component
- ERROR ReferenceError: google is not defined HOT 2
- Initialize with code instead of tag
- No directive found with exportAs 'ngx-places'.
- "google is not defined" in Angular 13 HOT 1
- Cannot Restrict Countries HOT 3
- There is no support for Angular 16! HOT 7
- Safari - iOS: When selecting the autocomplete address, it is not selected
- Not compatible with Angular 16 HOT 1
- When this library trigger google services?
- google map Key move from index.html HOT 1
- Dropdown Not Showing HOT 5
- Options Error - All fields required HOT 2
- Issues Returning Certain Addresses HOT 1
- Warning: CommonJS or AMD dependencies can cause optimization bailouts HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ngx-google-places-autocomplete.