fix: reject with error when url not loaded (#16571)

* fix: reject with error when url not loaded

* improve descriptive spec naming
This commit is contained in:
Shelley Vohr 2019-01-28 14:42:36 -08:00 committed by GitHub
parent 138ba53511
commit a25f82c91f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 5 deletions

View file

@ -1444,12 +1444,17 @@ v8::Local<v8::Promise> WebContents::HasServiceWorker() {
auto* context = GetServiceWorkerContext(web_contents());
if (!context) {
promise->RejectWithErrorMessage("Unable to get ServiceWorker context.");
return promise->GetHandle();
}
GURL url = web_contents()->GetLastCommittedURL();
if (!url.is_valid()) {
promise->RejectWithErrorMessage("URL invalid or not yet loaded.");
return promise->GetHandle();
}
context->CheckHasServiceWorker(
web_contents()->GetLastCommittedURL(),
web_contents()->GetLastCommittedURL(),
base::BindOnce(&OnServiceWorkerCheckDone, promise));
url, url, base::BindOnce(&OnServiceWorkerCheckDone, promise));
return promise->GetHandle();
}

View file

@ -229,13 +229,18 @@ describe('webContents module', () => {
})
describe('ServiceWorker APIs', () => {
it('can successfully register a ServiceWorker', async () => {
it('can successfully check for presence of a ServiceWorker', async () => {
await w.loadFile(path.join(fixtures, 'api', 'service-worker', 'service-worker.html'))
const hasSW = await w.webContents.hasServiceWorker()
expect(hasSW).to.be.true()
})
it('can successfully register a ServiceWorker (callback)', (done) => {
it('throws properly for invalid url', async () => {
const promise = w.webContents.hasServiceWorker()
return expect(promise).to.be.eventually.rejectedWith(Error, 'URL invalid or not yet loaded.')
})
it('can successfully check for presence of a ServiceWorker (callback)', (done) => {
w.loadFile(path.join(fixtures, 'api', 'service-worker', 'service-worker.html')).then(() => {
w.webContents.hasServiceWorker(hasSW => {
expect(hasSW).to.be.true()