analytics/test/plausible_web/plugs/tracker_test.exs
Adam Rutkowski 5de43b758d
Run tests in async mode where applicable (#2542)
* Set pg pool size for MIX_ENV=test

* Include slow tests in CI run

* Exclude slow tests by default

* Mark tests slow/async where applicable

* Restructure captcha mocks

* Revert async where env is relied upon

* Add --max-failures=1 to CI run

* Set warnings as errors

* Disable async where various mocks are used

* Revert "Disable async where various mocks are used"

This reverts commit 2446b72a29.

* Disable async for test using vcr
2022-12-26 10:20:29 -03:00

53 lines
1.8 KiB
Elixir

defmodule PlausibleWeb.TrackerTest do
@moduledoc """
This test module uses auto-generated JavaScript tracker files in priv/tracker/js.
To speed up running Elixir tests locally, the tracker files are not automatically generated
with every `mix test` command. That means, you have to manually run the below command
once before executing `mix test`. This is to make sure all tracker files are in place and up to date.
```
npm run deploy --prefix tracker
```
If you're making changes in the tracker template files (`src/plausible.js`, `compile.js`),
do regenerate the files before running tests, so they're up to date.
"""
use PlausibleWeb.ConnCase, async: true
use Plug.Test
test "returns legacy script p.js" do
assert String.contains?(get_script("p.js"), "; samesite=strict; path=/")
end
test "returns plausible script with every alias" do
plausible_js_response = get_script("plausible.js")
assert plausible_js_response == get_script("script.js")
assert plausible_js_response == get_script("analytics.js")
end
test "returns the right script extensions no matter the order" do
response = get_script("plausible.compat.file-downloads.hash.outbound-links.js")
assert String.contains?(response, "getElementById(\"plausible\")")
assert String.contains?(response, "file-types")
assert String.contains?(response, "hashchange")
assert String.contains?(response, "Outbound Link: Click")
assert !String.contains?(response, "data-exclude")
# local extension disabled
assert String.contains?(response, "/^localhost$|^127(\\.[0-9]+)")
assert response == get_script("plausible.outbound-links.file-downloads.compat.hash.js")
end
def get_script(filename) do
opts = PlausibleWeb.Tracker.init([])
conn(:get, "/js/#{filename}")
|> PlausibleWeb.Tracker.call(opts)
|> response(200)
end
end