Skip to content

Commit

Permalink
Merge pull request #9 from dysfunc/integration-tests
Browse files Browse the repository at this point in the history
✅ add rule and obstructions tests
  • Loading branch information
dysfunc authored Apr 1, 2021
2 parents ef554d2 + 2bf3c67 commit 0e1207a
Show file tree
Hide file tree
Showing 11 changed files with 283 additions and 91 deletions.
8 changes: 6 additions & 2 deletions cypress.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
"requestTimeout": 50000,
"responseTimeout": 50000,
"chromeWebSecurity": false,
"viewportHeight": 1050,
"viewportWidth": 1680
"viewportHeight": 768,
"viewportWidth": 1024,
"retries": {
"runMode": 3,
"openMode": 0
}
}
1 change: 1 addition & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<link rel="icon" type="image/png" href="/favicon.png" />
<link rel="stylesheet" href="/global.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/1.4.6/tailwind.min.css" />

<meta name="description" content="A simple rule-based approach to tracking element viewability." />
<meta name="keywords" content="viewability, viewable, tracking, ads, lazy, lazy-loading, svelte, svelte component, intersection observer, observer" />
Expand Down
2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"serve": "vite preview"
},
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "^1.0.0-next.5",
"@svelte-plugins/viewable": "../",
"@sveltejs/vite-plugin-svelte": "^1.0.0-next.5",
"autoprefixer": "^10.2.5",
"postcss": "^8.2.8",
"svelte": "^3.35.0",
Expand Down
149 changes: 126 additions & 23 deletions docs/src/App.svelte
Original file line number Diff line number Diff line change
@@ -1,40 +1,149 @@
<script>
import ContainerExample from './ContainerExample.svelte';
import ImageExample from "./ImageExample.svelte";
let enableObstructionDetection = false;
const handleClick = () => (enableObstructionDetection = !enableObstructionDetection);
</script>

<button id="obstructions" on:click={handleClick}>
<button id="obstructions" data-testid="toggle-obstructions" on:click={handleClick}>
Toggle Obstructions
</button>

Scroll down...

{#if enableObstructionDetection}
<div id="overlay"></div>
{/if}

<div class="container">
<p>Open dev console to see debug output.</p>
<ContainerExample enableObstructionDetection={enableObstructionDetection} />
<div class="container flex flex-col px-4 m-8 mx-auto space-y-4 sm:px-6 lg:px-8">
<div class="container">
<p class="badge">Open dev console to see debug output.</p>
</div>
<div class="flex justify-between">
<div class="w-1/2 mr-4 h-64 block"></div>
<div class="w-1/2 flex flex-col space-y-2">
<div class="w-full h-3 block"></div>
<div class="w-full h-2 block"></div>
<div class="w-full h-2 block"></div>
<div class="w-full h-2 block"></div>
<div class="w-full h-2 block"></div>
<div class="w-full h-2 block"></div>
<div class="w-3/4 h-2 block"></div>
</div>
</div>
<div class="flex flex-col space-y-2">
<div class="w-full h-2 block"></div>
<div class="w-full h-2 block"></div>
<div class="w-full h-2 block"></div>
<div class="w-full h-2 block"></div>
<div class="w-full h-2 block"></div>
<div class="w-full h-2 block"></div>
<div class="w-full h-2 block"></div>
<div class="w-1/5 h-2 block"></div>
</div>
<div class="flex">
<div class="flex-1 h-64 block" data-testid="top">
<ContainerExample enableObstructionDetection={enableObstructionDetection} />
</div>
</div>
<div class="flex flex-col w-4/6 space-y-2">
<div class="w-full h-2 block"></div>
<div class="w-full h-2 block"></div>
<div class="w-full h-2 block"></div>
<div class="w-3/5 h-2 block"></div>
</div>
<div class="flex items-stretch h-48">
<div class="flex-1 mr-4 block"></div>
<div class="flex-1 block"></div>
<div class="flex-1 ml-4 block"></div>
</div>
<div class="flex flex-col w-4/6 space-y-2">
<div class="w-full h-2 block"></div>
<div class="w-full h-2 block"></div>
<div class="w-full h-2 block"></div>
<div class="w-3/5 h-2 block"></div>
</div>
<div class="flex">
<div class="flex-1 h-64 block" data-testid="middle">
<ContainerExample enableObstructionDetection={enableObstructionDetection}>
Hello World!
</ContainerExample>
</div>
</div>
<div class="flex flex-col space-y-2">
<div class="w-full h-2 block"></div>
<div class="w-3/4 h-2 block"></div>
<div class="w-2/3 h-2 block"></div>
<div class="w-full h-2 block"></div>
<div class="w-full h-2 block"></div>
<div class="w-4/5 h-2 block"></div>
<div class="w-3/5 h-2 block"></div>
<div class="w-full h-2 block"></div>
</div>
<div class="flex items-stretch h-48">
<div class="flex-1 mr-2 block"></div>
<div class="flex-1 ml-2 block"></div>
</div>
<div class="flex items-stretch h-48">
<div class="flex-1 mr-4 block"></div>
<div class="flex-1 block" data-testid="bottom">
<ContainerExample />
</div>
<div class="flex-1 ml-4 block"></div>
</div>
<div class="flex flex-col space-y-2">
<div class="w-full h-2 block"></div>
<div class="w-full h-2 block"></div>
<div class="w-full h-2 block"></div>
</div>
<div class="flex flex-col w-4/6 space-y-2">
<div class="w-full h-2 block"></div>
<div class="w-full h-2 block"></div>
<div class="w-full h-2 block"></div>
<div class="w-3/5 h-2 block"></div>
</div>
<div class="flex items-stretch h-48">
<div class="flex-1 mr-4 block"></div>
<div class="flex-1 block"></div>
<div class="flex-1 ml-4 block"></div>
</div>
<div class="flex">
<div class="flex-1 h-64 block"></div>
</div>
<div class="flex flex-col w-4/6 space-y-2">
<div class="w-full h-2 block"></div>
<div class="w-full h-2 block"></div>
<div class="w-full h-2 block"></div>
<div class="w-3/5 h-2 block"></div>
</div>
</div>

<div class="container">
<ImageExample />
</div>
<style>
.badge {
background-color: #ffeaad;
border-radius: 4px;
display: inline-block;
color: #111;
display: inline-block;
padding: 8px 16px;
}
<div class="container">
<ContainerExample />
</div>
.block {
background: #f0f0f0;
}
.container {
min-width: 800px;
}
[data-testid="bottom"] {
font-size: .7em;
}
<style>
#obstructions {
padding: 8px 16px;
position: fixed;
right: 0;
top: 0;
right: 4px;
top: 4px;
}
#overlay {
Expand All @@ -59,7 +168,7 @@ Scroll down...
}
#overlay:after {
background: rgba(45, 45, 45, .1);
background: rgba(145, 145, 145, .1);
bottom: 25%;
content: " ";
left: 200px;
Expand All @@ -68,10 +177,4 @@ Scroll down...
top: 15%;
z-index: 99;
}
.container {
border-bottom: 1px solid #eee;
height: 100vh;
padding-top: 100vh;
}
</style>
64 changes: 45 additions & 19 deletions docs/src/ContainerExample.svelte
Original file line number Diff line number Diff line change
@@ -1,60 +1,86 @@
<script>
import Viewable from "@svelte-plugins/viewable";
export let enableObstructionDetection = false;
let events = [];
let element;
let duration;
let percent;
let percentX;
let percentY;
let activeRule = null;
let debug = true;
// you can use a single callback or separate callbacks for each rule
const fn = ({ percentage, duration, rule }) => {
console.log(`${percentage}% of the element was visible for at least ${duration} consecutive seconds.`);
events = [...events, `${percentage}% of the element was visible for at least ${duration} consecutive seconds. #${rule}`];
// update active rule
activeRule = rule;
}
const rules = {
// do something when this crosses the viewport
whenVisible: { duration: 2, percentage: 40, fn },
whenFourtyForTwo: { duration: 2, percentage: 40, fn },
// do something when this is 50% in view for 4 seconds
whenFiftyForOne: { duration: 4, percentage: 50, fn },
whenFiftyForFour: { duration: 4, percentage: 50, fn },
// do something when this is 100% in view for 6 seconds
whenHundredForFour: { duration: 6, percentage: 100, fn }
whenHundredForSix: { duration: 6, percentage: 100, fn }
};
export let enableObstructionDetection = false;
let element;
let duration;
let percent;
let percentX;
let percentY;
let activeRule = null;
let debug = true;
</script>

<Viewable bind:duration bind:percent bind:percentY bind:percentX {rules} {element} {enableObstructionDetection} {debug}>
<div bind:this={element} class={`${activeRule}`}>
<slot></slot>

<p>Duration: {duration}s</p>
<p>Viewable area: {percent}% x: {percentX}% y: {percentY}%</p>

{#if events.length}
{#each events as event}
<p>{event}</p>
{/each}
{/if}
</div>
</Viewable>

<style>
div {
background-color: #ccc;
background-color: #ddd;
border-radius: 2px;
height: 100%;
padding: 20px;
height: 25vh;
}
div.whenVisible {
div.whenFourtyForTwo {
background-color: #50e3c2;
color: #fff;
}
div.whenFiftyForOne {
div.whenFiftyForFour {
background-color: #4a90e2;
color: #fff;
}
div.whenHundredForFour {
div.whenHundredForSix {
background-color: #fd8e82;
color: #fff;
position: relative;
}
div.whenHundredForSix:before {
background: rgba(255,255,255, .75);
border-radius: 100%;
bottom: 20px;
content: "🎉";
height: 100px;
font-size: 3em;
left: calc(50% - 50px);
line-height: 100px;
position: absolute;
right: 0;
text-align: center;
top: calc(50% - 50px);
width: 100px;
}
</style>
32 changes: 0 additions & 32 deletions docs/src/ImageExample.svelte

This file was deleted.

12 changes: 0 additions & 12 deletions tests/integration/index.spec.js

This file was deleted.

Loading

0 comments on commit 0e1207a

Please sign in to comment.