Skip to content

Update the test:setup script to configure firestore settings #240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,26 @@ $ yarn
A production project is required to test the Firebase JS SDK. You can create a
new project by visiting the [Firebase Console](https://console.firebase.google.com/).

#### Firestore Support

Visit the database section of the console and enable the Cloud Firestore Beta.
You can select either of the default permissions settings as we will overwrite
them below.

#### Authentication Support

Visit the authentication config in your project and enable the `Anonymous`
sign-in provider to complete your project config.

#### Automated Setup

Most of the test setup can be done by running the following command at the root
of the package:
The remainder of the test setup can be done by running the following command at
the root of the package:

```bash
yarn test:setup
```

#### Authentication Support

This is the only piece of config that must be done manually. Visit the
authentication config in your project and enable the `Anonymous` sign-in
provider to complete your project config.

### Running the tests

Each of the directories in the `integration` directory as well as the `packages`
Expand Down
6 changes: 5 additions & 1 deletion config/firebase.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"database": {
"rules": "database.rules.json"
},
"firestore": {
"rules": "firestore.rules",
"indexes": "firestore.indexes.json"
}
}
}
3 changes: 3 additions & 0 deletions config/firestore.indexes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"indexes": []
}
7 changes: 7 additions & 0 deletions config/firestore.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write;
}
}
}
55 changes: 26 additions & 29 deletions packages/messaging/test/controller-deleteToken.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ describe('Firebase Messaging > *Controller.deleteToken()', function() {
});

const configureRegistrationMocks = (ServiceClass, fakeReg) => {
sandbox.stub(
ServiceClass.prototype,
'getSWRegistration_'
).callsFake(() => {
sandbox.stub(ServiceClass.prototype, 'getSWRegistration_').callsFake(() => {
return fakeReg;
});
};
Expand All @@ -59,7 +56,7 @@ describe('Firebase Messaging > *Controller.deleteToken()', function() {
}

return getSubResult;
},
}
}
});
return Promise.resolve(registration);
Expand All @@ -75,8 +72,8 @@ describe('Firebase Messaging > *Controller.deleteToken()', function() {
}
return Promise.all(deletePromises)
.then(() => deleteDatabase(TokenManager.DB_NAME))
.then(() => globalMessagingService = null);
}
.then(() => (globalMessagingService = null));
};

beforeEach(function() {
return cleanUp();
Expand All @@ -88,8 +85,7 @@ describe('Firebase Messaging > *Controller.deleteToken()', function() {

it('should handle no token to delete', function() {
globalMessagingService = new WindowController(app);
return globalMessagingService.deleteToken()
.then(
return globalMessagingService.deleteToken().then(
() => {
throw new Error('Expected error to be thrown.');
},
Expand All @@ -105,15 +101,15 @@ describe('Firebase Messaging > *Controller.deleteToken()', function() {
it('should handle no registration', function() {
configureRegistrationMocks(WindowController, Promise.resolve(null));

return dbTMHelper.addObjectToIndexDB(EXAMPLE_TOKEN_SAVE)
.then(() => {
return dbTMHelper.addObjectToIndexDB(EXAMPLE_TOKEN_SAVE).then(() => {
globalMessagingService = new WindowController(app);
return globalMessagingService.deleteToken(EXAMPLE_TOKEN_SAVE.fcmToken);
});
});

it('should handle get subscription error', function() {
configureRegistrationMocks(WindowController,
configureRegistrationMocks(
WindowController,
generateFakeReg(() => Promise.reject(new Error('Unknown error')))
);

Expand All @@ -130,12 +126,14 @@ describe('Firebase Messaging > *Controller.deleteToken()', function() {
);
});

[WindowController, SWController].forEach((ServiceClass) => {
[WindowController, SWController].forEach(ServiceClass => {
it(`should handle null getSubscription() ${ServiceClass.name}`, function() {
configureRegistrationMocks(ServiceClass, generateFakeReg(Promise.resolve(null)));
configureRegistrationMocks(
ServiceClass,
generateFakeReg(Promise.resolve(null))
);

return dbTMHelper.addObjectToIndexDB(EXAMPLE_TOKEN_SAVE)
.then(() => {
return dbTMHelper.addObjectToIndexDB(EXAMPLE_TOKEN_SAVE).then(() => {
globalMessagingService = new ServiceClass(app);
return globalMessagingService.deleteToken(EXAMPLE_TOKEN_SAVE.fcmToken);
});
Expand All @@ -153,18 +151,18 @@ describe('Firebase Messaging > *Controller.deleteToken()', function() {
generateFakeReg(Promise.resolve(fakeSubscription))
);

return dbTMHelper.addObjectToIndexDB(EXAMPLE_TOKEN_SAVE)
.then(() => {
return dbTMHelper.addObjectToIndexDB(EXAMPLE_TOKEN_SAVE).then(() => {
globalMessagingService = new ServiceClass(app);
return globalMessagingService.deleteToken(EXAMPLE_TOKEN_SAVE.fcmToken)
.then(
() => {
throw new Error('Expected this to reject');
},
err => {
assert.equal(errorMsg, err.message);
}
);
return globalMessagingService
.deleteToken(EXAMPLE_TOKEN_SAVE.fcmToken)
.then(
() => {
throw new Error('Expected this to reject');
},
err => {
assert.equal(errorMsg, err.message);
}
);
});
});

Expand All @@ -179,8 +177,7 @@ describe('Firebase Messaging > *Controller.deleteToken()', function() {
generateFakeReg(Promise.resolve(fakeSubscription))
);

return dbTMHelper.addObjectToIndexDB(EXAMPLE_TOKEN_SAVE)
.then(() => {
return dbTMHelper.addObjectToIndexDB(EXAMPLE_TOKEN_SAVE).then(() => {
globalMessagingService = new ServiceClass(app);
return globalMessagingService.deleteToken(EXAMPLE_TOKEN_SAVE.fcmToken);
});
Expand Down
10 changes: 7 additions & 3 deletions packages/messaging/test/get-sw-reg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ describe('Firebase Messaging > *Controller.getSWReg_()', function() {
const sandbox = sinon.sandbox.create();

const mockWindowRegistration = registration => {
sandbox.stub(navigator.serviceWorker, 'register')
sandbox
.stub(navigator.serviceWorker, 'register')
.callsFake(() => Promise.resolve(registration));
};

Expand All @@ -42,7 +43,7 @@ describe('Firebase Messaging > *Controller.getSWReg_()', function() {

beforeEach(function() {
return cleanUp();
})
});

after(function() {
return cleanUp();
Expand Down Expand Up @@ -111,7 +112,10 @@ describe('Firebase Messaging > *Controller.getSWReg_()', function() {

it('should make registration error available to developer', function() {
const errorMsg = 'test-reg-error-1234567890';
const mockRegisterMethod = sandbox.stub(navigator.serviceWorker, 'register');
const mockRegisterMethod = sandbox.stub(
navigator.serviceWorker,
'register'
);
mockRegisterMethod.callsFake(() => Promise.reject(new Error(errorMsg)));

const messagingService = new WindowController(app);
Expand Down
62 changes: 28 additions & 34 deletions packages/messaging/test/get-token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ describe('Firebase Messaging > *Controller.getToken()', function() {

const mockGetReg = fakeReg => {
servicesToTest.forEach(serviceClass => {
sandbox.stub(
serviceClass.prototype,
'getSWRegistration_'
)
.callsFake(() => fakeReg);
sandbox
.stub(serviceClass.prototype, 'getSWRegistration_')
.callsFake(() => fakeReg);
});
};

Expand All @@ -52,18 +50,16 @@ describe('Firebase Messaging > *Controller.getToken()', function() {

beforeEach(function() {
return cleanUp();
})
});

after(function() {
return cleanUp();
});

it('should throw on unsupported browsers', function() {
sandbox.stub(
WindowController.prototype,
'isSupported_'
)
.callsFake(() => false);
sandbox
.stub(WindowController.prototype, 'isSupported_')
.callsFake(() => false);

const messagingService = new WindowController(app);
return messagingService.getToken().then(
Expand All @@ -77,14 +73,13 @@ describe('Firebase Messaging > *Controller.getToken()', function() {
});

it('should handle a failure to get registration', function() {
sandbox.stub(
ControllerInterface.prototype,
'getNotificationPermission_'
)
.callsFake(() => NotificationPermission.granted);
sandbox
.stub(ControllerInterface.prototype, 'getNotificationPermission_')
.callsFake(() => NotificationPermission.granted);

sandbox.stub(navigator.serviceWorker, 'register')
.callsFake(() => Promise.reject('No Service Worker'));
sandbox
.stub(navigator.serviceWorker, 'register')
.callsFake(() => Promise.reject('No Service Worker'));

const messagingService = new WindowController(app);
return messagingService
Expand Down Expand Up @@ -144,16 +139,15 @@ describe('Firebase Messaging > *Controller.getToken()', function() {
it('should get saved token', function() {
const registration = makeFakeSWReg();

sandbox.stub(
ControllerInterface.prototype,
'getNotificationPermission_'
)
.callsFake(() => NotificationPermission.granted);
sandbox
.stub(ControllerInterface.prototype, 'getNotificationPermission_')
.callsFake(() => NotificationPermission.granted);

mockGetReg(Promise.resolve(registration));

sandbox.stub(TokenManager.prototype, 'getSavedToken')
.callsFake(() => Promise.resolve(EXAMPLE_FCM_TOKEN));
sandbox
.stub(TokenManager.prototype, 'getSavedToken')
.callsFake(() => Promise.resolve(EXAMPLE_FCM_TOKEN));

return Promise.all(
servicesToTest.map(ServiceClass => {
Expand All @@ -168,19 +162,19 @@ describe('Firebase Messaging > *Controller.getToken()', function() {
it('should get a new token', function() {
const registration = makeFakeSWReg();

sandbox.stub(
ControllerInterface.prototype,
'getNotificationPermission_'
)
.callsFake(() => NotificationPermission.granted);
sandbox
.stub(ControllerInterface.prototype, 'getNotificationPermission_')
.callsFake(() => NotificationPermission.granted);

mockGetReg(Promise.resolve(registration));

sandbox.stub(TokenManager.prototype, 'getSavedToken')
.callsFake(() => Promise.resolve(null));
sandbox
.stub(TokenManager.prototype, 'getSavedToken')
.callsFake(() => Promise.resolve(null));

sandbox.stub(TokenManager.prototype, 'createToken')
.callsFake(() => Promise.resolve(EXAMPLE_FCM_TOKEN));
sandbox
.stub(TokenManager.prototype, 'createToken')
.callsFake(() => Promise.resolve(EXAMPLE_FCM_TOKEN));

return Promise.all(
servicesToTest.map(ServiceClass => {
Expand Down
4 changes: 2 additions & 2 deletions packages/messaging/test/testing-utils/db-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const deleteDatabase = (dbName) => {
const deleteDatabase = dbName => {
return new Promise((resolve, reject) => {
const request = indexedDB.deleteDatabase(dbName);
request.onerror = event => {
Expand All @@ -26,5 +26,5 @@ const deleteDatabase = (dbName) => {
console.warn('deleteDb blocked.');
};
});
}
};
export { deleteDatabase };
2 changes: 1 addition & 1 deletion packages/messaging/test/testing-utils/db-token-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,5 @@ export default {
};
});
});
},
}
};
14 changes: 7 additions & 7 deletions packages/messaging/test/tokenDetailsModel-deleteToken.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ describe('Firebase Messaging > TokenDetailsModel.deleteToken()', function() {

return Promise.all(promises)
.then(() => deleteDatabase(TokenDetailsModel.dbName))
.then(() => globalTokenModel = null);
}
.then(() => (globalTokenModel = null));
};

beforeEach(function() {
return cleanUp();
Expand All @@ -56,8 +56,7 @@ describe('Firebase Messaging > TokenDetailsModel.deleteToken()', function() {

it('should handle no input', function() {
globalTokenModel = new TokenDetailsModel();
return globalTokenModel.deleteToken()
.then(
return globalTokenModel.deleteToken().then(
() => {
throw new Error('Expected this to throw an error due to no token');
},
Expand All @@ -72,8 +71,7 @@ describe('Firebase Messaging > TokenDetailsModel.deleteToken()', function() {

it('should handle empty string', function() {
globalTokenModel = new TokenDetailsModel();
return globalTokenModel.deleteToken('')
.then(
return globalTokenModel.deleteToken('').then(
() => {
throw new Error('Expected this to throw an error due to no token');
},
Expand Down Expand Up @@ -115,7 +113,9 @@ describe('Firebase Messaging > TokenDetailsModel.deleteToken()', function() {
assert.equal(details[keyName], EXAMPLE_INPUT[keyName]);
});

return globalTokenModel.getTokenDetailsFromToken(EXAMPLE_INPUT.fcmToken);
return globalTokenModel.getTokenDetailsFromToken(
EXAMPLE_INPUT.fcmToken
);
})
.then(tokenDetails => {
assert.equal(null, tokenDetails);
Expand Down
Loading