Skip to content

Commit

Permalink
Add unit tests for the native adapter interface, so we can ensure tha…
Browse files Browse the repository at this point in the history
…t the proper methods are called for the native adapters
  • Loading branch information
jayohms committed Oct 31, 2023
1 parent a0f4583 commit 2414740
Showing 1 changed file with 201 additions and 0 deletions.
201 changes: 201 additions & 0 deletions src/tests/unit/native_adapter_support_tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
import * as Turbo from "../../index"
import { assert } from "@open-wc/testing"

class NativeAdapterSupportTest {
proposedVisits = []
startedVisits = []
completedVisits = []
startedVisitRequests = []
completedVisitRequests = []
failedVisitRequests = []
finishedVisitRequests = []
startedFormSubmissions = []
finishedFormSubmissions = []

// Adapter interface

visitProposedToLocation(location, options) {
this.proposedVisits.push({ location, options })
}

visitStarted(visit) {
this.startedVisits.push(visit)
}

visitCompleted(visit) {
this.completedVisits.push(visit)
}

visitRequestStarted(visit) {
this.startedVisitRequests.push(visit)
}

visitRequestCompleted(visit) {
this.completedVisitRequests.push(visit)
}

visitRequestFailedWithStatusCode(visit, _statusCode) {
this.failedVisitRequests.push(visit)
}

visitRequestFinished(visit) {
this.finishedVisitRequests.push(visit)
}

visitRendered(_visit) {}

formSubmissionStarted(formSubmission) {
this.startedFormSubmissions.push(formSubmission)
}

formSubmissionFinished(formSubmission) {
this.finishedFormSubmissions.push(formSubmission)
}

pageInvalidated() {}
}

let adapter

setup(() => {
adapter = new NativeAdapterSupportTest()
Turbo.registerAdapter(adapter)
})

test("test navigator adapter is native adapter", async () => {
assert.equal(adapter, Turbo.navigator.adapter)
})

test("test visit proposal location is proposed to adapter", async () => {
const url = new URL(window.location.toString())

Turbo.navigator.proposeVisit(url)
assert.equal(adapter.proposedVisits.length, 1)

const [visit] = adapter.proposedVisits
assert.equal(visit.location, url)
})

test("test visit proposal external location is proposed to adapter", async () => {
const url = new URL("https://example.com/")

Turbo.navigator.proposeVisit(url)
assert.equal(adapter.proposedVisits.length, 1)

const [visit] = adapter.proposedVisits
assert.equal(visit.location, url)
})

test("test visit started notifies adapter", async () => {
const locatable = window.location.toString()

Turbo.navigator.startVisit(locatable)
assert.equal(adapter.startedVisits.length, 1)

const [visit] = adapter.startedVisits
assert.equal(visit.location, locatable)
})

test("test visit completed notifies adapter", async () => {
const locatable = window.location.toString()

Turbo.navigator.startVisit(locatable)

const [startedVisit] = adapter.startedVisits
startedVisit.complete()

const [completedVisit] = adapter.completedVisits
assert.equal(completedVisit.location, locatable)
})

test("test visit request started notifies adapter", async () => {
const locatable = window.location.toString()

Turbo.navigator.startVisit(locatable)

const [startedVisit] = adapter.startedVisits
startedVisit.startRequest()
assert.equal(adapter.startedVisitRequests.length, 1)

const [startedVisitRequest] = adapter.startedVisitRequests
assert.equal(startedVisitRequest.location, locatable)
})

test("test visit request completed notifies adapter", async () => {
const locatable = window.location.toString()

Turbo.navigator.startVisit(locatable)

const [startedVisit] = adapter.startedVisits
startedVisit.recordResponse({ statusCode: 200, responseHTML: "responseHtml", redirected: false })
assert.equal(adapter.completedVisitRequests.length, 1)

const [completedVisitRequest] = adapter.completedVisitRequests
assert.equal(completedVisitRequest.location, locatable)
})

test("test visit request failed notifies adapter", async () => {
const locatable = window.location.toString()

Turbo.navigator.startVisit(locatable)

const [startedVisit] = adapter.startedVisits
startedVisit.recordResponse({ statusCode: 404, responseHTML: "responseHtml", redirected: false })
assert.equal(adapter.failedVisitRequests.length, 1)

const [failedVisitRequest] = adapter.failedVisitRequests
assert.equal(failedVisitRequest.location, locatable)
})

test("test visit request finished notifies adapter", async () => {
const locatable = window.location.toString()

Turbo.navigator.startVisit(locatable)

const [startedVisit] = adapter.startedVisits
startedVisit.finishRequest()
assert.equal(adapter.finishedVisitRequests.length, 1)

const [finishedVisitRequest] = adapter.finishedVisitRequests
assert.equal(finishedVisitRequest.location, locatable)
})

test("test form submission started notifies adapter", async () => {
const url = new URL(window.location.toString())

Check failure on line 164 in src/tests/unit/native_adapter_support_tests.js

View workflow job for this annotation

GitHub Actions / build

'url' is assigned a value but never used

Check failure on line 164 in src/tests/unit/native_adapter_support_tests.js

View workflow job for this annotation

GitHub Actions / build

'url' is assigned a value but never used

Turbo.navigator.formSubmissionStarted("formSubmissionStub")
assert.equal(adapter.startedFormSubmissions.length, 1)

const [startedFormSubmission] = adapter.startedFormSubmissions
assert.equal(startedFormSubmission, "formSubmissionStub")
})

test("test form submission finished notifies adapter", async () => {
const url = new URL(window.location.toString())

Check failure on line 174 in src/tests/unit/native_adapter_support_tests.js

View workflow job for this annotation

GitHub Actions / build

'url' is assigned a value but never used

Check failure on line 174 in src/tests/unit/native_adapter_support_tests.js

View workflow job for this annotation

GitHub Actions / build

'url' is assigned a value but never used

Turbo.navigator.formSubmissionFinished("formSubmissionStub")
assert.equal(adapter.finishedFormSubmissions.length, 1)

const [finishedFormSubmission] = adapter.finishedFormSubmissions
assert.equal(finishedFormSubmission, "formSubmissionStub")
})


test("test visit follows redirect and proposes replace visit to adapter", async () => {
const locatable = window.location.toString()
const redirectedLocation = "https://example.com"

Turbo.navigator.startVisit(locatable)

const [startedVisit] = adapter.startedVisits
startedVisit.redirectedToLocation = redirectedLocation
startedVisit.recordResponse({ statusCode: 200, responseHTML: "responseHtml", redirected: true })
startedVisit.complete()

assert.equal(adapter.completedVisitRequests.length, 1)
assert.equal(adapter.proposedVisits.length, 1)

const [visit] = adapter.proposedVisits
assert.equal(visit.location, redirectedLocation)
assert.equal(visit.options.action, "replace")
})

0 comments on commit 2414740

Please sign in to comment.