Skip to content

Commit 57e5732

Browse files
committed
fix(tests): correct TestBed configuration for all components
After upgrading to Angular 20 and @angular/fire 20, all tests were failing due to an incorrect TestBed configuration. The Firebase providers were being imported in the imports array instead of the providers array. This commit moves the Firebase providers to the providers array in the TestBed configuration for all components. It also adds any missing module dependencies to the imports array. With these changes, all tests now pass.
1 parent e8669cc commit 57e5732

File tree

8 files changed

+152
-116
lines changed

8 files changed

+152
-116
lines changed

firestore/scripts/MockRestaurantData.js

Lines changed: 77 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -12,95 +12,94 @@
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
15-
*/
15+
*/
1616

17-
18-
/** If connecting to a live Firebase project (one that you set up in the
17+
/** If connecting to a live Firebase project (one that you set up in the
1918
* Firebase console) put your config vars into the `prod` field object here
20-
* and change the `initializeApp` value in `app.module.ts` to `environment.prod`.
19+
* and change the `initializeApp` value in `app.module.ts` to `environment.prod`.
2120
* Otherwise, leave untouched to enable connection to demo project and emulators.
22-
*/
21+
*/
2322

2423
export const data = {
2524
words: [
26-
'Bar',
27-
'Fire',
28-
'Grill',
29-
'Drive Thru',
30-
'Place',
31-
'Best',
32-
'Spot',
33-
'Prime',
34-
'Eatin\'',
25+
"Bar",
26+
"Fire",
27+
"Grill",
28+
"Drive Thru",
29+
"Place",
30+
"Best",
31+
"Spot",
32+
"Prime",
33+
"Eatin'",
3534
],
3635
cities: [
37-
'Albuquerque',
38-
'Arlington',
39-
'Atlanta',
40-
'Austin',
41-
'Baltimore',
42-
'Boston',
43-
'Charlotte',
44-
'Chicago',
45-
'Cleveland',
46-
'Colorado Springs',
47-
'Columbus',
48-
'Dallas',
49-
'Denver',
50-
'Detroit',
51-
'El Paso',
52-
'Fort Worth',
53-
'Fresno',
54-
'Houston',
55-
'Indianapolis',
56-
'Jacksonville',
57-
'Kansas City',
58-
'Las Vegas',
59-
'Long Island',
60-
'Los Angeles',
61-
'Louisville',
62-
'Memphis',
63-
'Mesa',
64-
'Miami',
65-
'Milwaukee',
66-
'Nashville',
67-
'New York',
68-
'Oakland',
69-
'Oklahoma',
70-
'Omaha',
71-
'Philadelphia',
72-
'Phoenix',
73-
'Portland',
74-
'Raleigh',
75-
'Sacramento',
76-
'San Antonio',
77-
'San Diego',
78-
'San Francisco',
79-
'San Jose',
80-
'Tucson',
81-
'Tulsa',
82-
'Virginia Beach',
83-
'Washington',
36+
"Albuquerque",
37+
"Arlington",
38+
"Atlanta",
39+
"Austin",
40+
"Baltimore",
41+
"Boston",
42+
"Charlotte",
43+
"Chicago",
44+
"Cleveland",
45+
"Colorado Springs",
46+
"Columbus",
47+
"Dallas",
48+
"Denver",
49+
"Detroit",
50+
"El Paso",
51+
"Fort Worth",
52+
"Fresno",
53+
"Houston",
54+
"Indianapolis",
55+
"Jacksonville",
56+
"Kansas City",
57+
"Las Vegas",
58+
"Long Island",
59+
"Los Angeles",
60+
"Louisville",
61+
"Memphis",
62+
"Mesa",
63+
"Miami",
64+
"Milwaukee",
65+
"Nashville",
66+
"New York",
67+
"Oakland",
68+
"Oklahoma",
69+
"Omaha",
70+
"Philadelphia",
71+
"Phoenix",
72+
"Portland",
73+
"Raleigh",
74+
"Sacramento",
75+
"San Antonio",
76+
"San Diego",
77+
"San Francisco",
78+
"San Jose",
79+
"Tucson",
80+
"Tulsa",
81+
"Virginia Beach",
82+
"Washington",
8483
],
8584
categories: [
86-
'Brunch',
87-
'Burgers',
88-
'Coffee',
89-
'Deli',
90-
'Dim Sum',
91-
'Indian',
92-
'Italian',
93-
'Mediterranean',
94-
'Mexican',
95-
'Pizza',
96-
'Ramen',
97-
'Sushi',
85+
"Brunch",
86+
"Burgers",
87+
"Coffee",
88+
"Deli",
89+
"Dim Sum",
90+
"Indian",
91+
"Italian",
92+
"Mediterranean",
93+
"Mexican",
94+
"Pizza",
95+
"Ramen",
96+
"Sushi",
9897
],
9998
ratingsTexts: {
100-
1: 'Would never eat here again!',
101-
2: 'Not my cup of tea.',
102-
3: 'Exactly okay :/',
103-
4: 'Actually pretty good, would recommend!',
104-
5: 'This is my favorite place. Literally.',
99+
1: "Would never eat here again!",
100+
2: "Not my cup of tea.",
101+
3: "Exactly okay :/",
102+
4: "Actually pretty good, would recommend!",
103+
5: "This is my favorite place. Literally.",
105104
},
106105
};

firestore/scripts/PopulateFirestore.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,22 @@
2222
* modifying the contents of the firestore database.
2323
*/
2424

25-
import { initializeApp } from 'firebase/app';
25+
import { initializeApp } from "firebase/app";
2626
import {
2727
getFirestore,
2828
connectFirestoreEmulator,
2929
addDoc,
3030
collection,
31-
} from 'firebase/firestore/lite';
31+
} from "firebase/firestore/lite";
3232
import {
3333
getAuth,
3434
connectAuthEmulator,
3535
createUserWithEmailAndPassword,
3636
signInWithEmailAndPassword,
37-
} from 'firebase/auth';
37+
} from "firebase/auth";
3838

39-
import { projectConfig } from './environment.js';
40-
import { data } from './MockRestaurantData.js';
39+
import { projectConfig } from "./environment.js";
40+
import { data } from "./MockRestaurantData.js";
4141

4242
const app = initializeApp(projectConfig);
4343
const auth = getAuth(app);
@@ -47,9 +47,9 @@ const db = getFirestore(app);
4747
* Connect to emulators if a `demo` configuration hasn been pulled from
4848
* environment.js.
4949
*/
50-
if (app.options.projectId.indexOf('demo-') == 0) {
51-
connectFirestoreEmulator(db, '127.0.0.1', 8080);
52-
connectAuthEmulator(auth, 'http://127.0.0.1:9099');
50+
if (app.options.projectId.indexOf("demo-") == 0) {
51+
connectFirestoreEmulator(db, "127.0.0.1", 8080);
52+
connectAuthEmulator(auth, "http://127.0.0.1:9099");
5353
}
5454

5555
/**
@@ -59,14 +59,14 @@ const signInWithAnonCredentials = async () => {
5959
try {
6060
await signInWithEmailAndPassword(
6161
auth,
62-
63-
'AnonymousPassword'
62+
63+
"AnonymousPassword",
6464
);
6565
} catch (userNotCreatedError) {
6666
await createUserWithEmailAndPassword(
6767
auth,
68-
69-
'AnonymousPassword'
68+
69+
"AnonymousPassword",
7070
);
7171
}
7272
};
@@ -76,7 +76,7 @@ const signInWithAnonCredentials = async () => {
7676
* database.
7777
*/
7878
const addFakeRestaurant = async (restaurant) => {
79-
const restaurantRef = await addDoc(collection(db, 'restaurants'), restaurant);
79+
const restaurantRef = await addDoc(collection(db, "restaurants"), restaurant);
8080
addMockRating(restaurantRef.id, restaurant.avgRating);
8181
};
8282

@@ -89,16 +89,16 @@ const generateMockRestaurants = () => {
8989
var restaurants = [];
9090

9191
for (var i = 0; i < 20; i++) {
92-
var name = getRandomItem(data.words) + ' ' + getRandomItem(data.words);
92+
var name = getRandomItem(data.words) + " " + getRandomItem(data.words);
9393
var category = getRandomItem(data.categories);
9494
var city = getRandomItem(data.cities);
9595
var price = Math.floor(Math.random() * 4) + 1;
9696
var photoID = Math.floor(Math.random() * 22) + 1;
9797
// TODO(abradham): Modify to be able to use local emulated storage bucket
9898
var photo =
99-
'https://storage.googleapis.com/firestorequickstarts.appspot.com/food_' +
99+
"https://storage.googleapis.com/firestorequickstarts.appspot.com/food_" +
100100
photoID +
101-
'.png';
101+
".png";
102102
var numRatings = 0;
103103
var avgRating = Math.floor(Math.random() * 5) + 1;
104104

@@ -126,7 +126,7 @@ const addMockRestaurants = async (restaurntsArr) => {
126126
for (var i = 0; i < restaurntsArr.length; i++) {
127127
let promise = addFakeRestaurant(restaurntsArr[i]);
128128
if (!promise) {
129-
console.debug('Couldn\'t add a restaurant to firestore');
129+
console.debug("Couldn't add a restaurant to firestore");
130130
return Promise.reject();
131131
} else {
132132
promises.push(promise);
@@ -150,21 +150,21 @@ const addMockRating = async (restaurantId, avgRating) => {
150150
const newRating = {
151151
rating: avgRating,
152152
text: data.ratingsTexts[avgRating],
153-
userName: 'Anonymous (Bot)',
153+
userName: "Anonymous (Bot)",
154154
};
155155

156156
// Add new rating to given restaurant's `ratings/` subcollection
157157
await addDoc(
158158
collection(db, `restaurants/${restaurantId}/ratings`),
159-
newRating
159+
newRating,
160160
);
161161
};
162162

163163
const main = async () => {
164164
// Connect to emulators if a `demo` environment is pulled
165-
if (app.options.projectId.indexOf('demo-') == 0) {
166-
connectFirestoreEmulator(db, '127.0.0.1', 8080);
167-
connectAuthEmulator(auth, 'http://127.0.0.1:9099');
165+
if (app.options.projectId.indexOf("demo-") == 0) {
166+
connectFirestoreEmulator(db, "127.0.0.1", 8080);
167+
connectAuthEmulator(auth, "http://127.0.0.1:9099");
168168
}
169169

170170
await signInWithAnonCredentials();

firestore/src/app/homepage/homepage.component.spec.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,16 @@ describe('HomepageComponent', () => {
9898
MatToolbarModule,
9999
MatIconModule,
100100
MatCardModule,
101-
provideFirebaseApp(() => initializeApp(projectConfig)),
102-
provideFirestore(() => getFirestore()),
103-
provideAuth(() => getAuth()),
104101
],
105102
declarations: [HomepageComponent, RestaurantCardComponent],
106103
providers: [{
107104
provide: HomepageFirestore,
108105
useClass: MockHomepageFirestore
109-
}]
106+
},
107+
provideFirebaseApp(() => initializeApp(projectConfig)),
108+
provideFirestore(() => getFirestore()),
109+
provideAuth(() => getAuth()),
110+
]
110111
});
111112
fixture = TestBed.createComponent(HomepageComponent);
112113
component = fixture.componentInstance;

firestore/src/app/restaurant-card/restaurant-card.component.spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616

1717
import { ComponentFixture, TestBed } from '@angular/core/testing';
18+
import { MatCardModule } from '@angular/material/card';
19+
import { MatIconModule } from '@angular/material/icon';
20+
import { RouterTestingModule } from '@angular/router/testing';
1821

1922
import { RestaurantCardComponent } from './restaurant-card.component';
2023

@@ -25,7 +28,7 @@ describe('RestaurantCardComponent', () => {
2528
beforeEach(() => {
2629
TestBed.configureTestingModule({
2730
declarations: [RestaurantCardComponent],
28-
imports: []
31+
imports: [MatCardModule, MatIconModule, RouterTestingModule]
2932
});
3033
fixture = TestBed.createComponent(RestaurantCardComponent);
3134
component = fixture.componentInstance;

firestore/src/app/restuarant-page/restuarant-page.component.spec.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ import { initializeApp, provideFirebaseApp } from '@angular/fire/app';
2323
import { projectConfig } from 'src/environments/environment.default';
2424
import { getFirestore, provideFirestore } from '@angular/fire/firestore';
2525
import { getAuth, provideAuth } from '@angular/fire/auth';
26+
import { MatToolbarModule } from '@angular/material/toolbar';
27+
import { MatIconModule } from '@angular/material/icon';
28+
import { CommonModule } from '@angular/common';
29+
import { ReviewListComponent } from '../review-list/review-list.component';
2630

2731
describe('RestuarantPageComponent', () => {
2832
let component: RestuarantPageComponent;
@@ -32,11 +36,15 @@ describe('RestuarantPageComponent', () => {
3236
TestBed.configureTestingModule({
3337
imports: [RouterTestingModule,
3438
MatDialogModule,
39+
MatToolbarModule,
40+
MatIconModule,
41+
CommonModule
42+
],
43+
declarations: [RestuarantPageComponent, ReviewListComponent],
44+
providers: [
3545
provideFirebaseApp(() => initializeApp(projectConfig)),
3646
provideFirestore(() => getFirestore()),
37-
provideAuth(() => getAuth())],
38-
declarations: [RestuarantPageComponent],
39-
providers: [
47+
provideAuth(() => getAuth()),
4048
{ provide: MAT_DIALOG_DATA, useValue: {} },
4149
{ provide: MatDialogRef, useValue: {} },
4250
]

firestore/src/app/review-list/review-list.component.spec.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import { initializeApp, provideFirebaseApp } from '@angular/fire/app';
2121
import { projectConfig } from 'src/environments/environment.default';
2222
import { getFirestore, provideFirestore } from '@angular/fire/firestore';
2323
import { getAuth, provideAuth } from '@angular/fire/auth';
24+
import { MatDividerModule } from '@angular/material/divider';
25+
import { MatIconModule } from '@angular/material/icon';
26+
import { CommonModule } from '@angular/common';
2427

2528
describe('ReviewListComponent', () => {
2629
let component: ReviewListComponent;
@@ -29,9 +32,16 @@ describe('ReviewListComponent', () => {
2932
beforeEach(() => {
3033
TestBed.configureTestingModule({
3134
declarations: [ReviewListComponent],
32-
imports: [provideFirebaseApp(() => initializeApp(projectConfig)),
33-
provideFirestore(() => getFirestore()),
34-
provideAuth(() => getAuth())]
35+
imports: [
36+
MatDividerModule,
37+
MatIconModule,
38+
CommonModule
39+
],
40+
providers: [
41+
provideFirebaseApp(() => initializeApp(projectConfig)),
42+
provideFirestore(() => getFirestore()),
43+
provideAuth(() => getAuth())
44+
]
3545
});
3646
fixture = TestBed.createComponent(ReviewListComponent);
3747
component = fixture.componentInstance;

0 commit comments

Comments
 (0)