Skip to content

Commit 09f3660

Browse files
author
Dennis Labordus
committed
Added some simple testing for what is possible.
Signed-off-by: Dennis Labordus <[email protected]>
1 parent 8cf5c34 commit 09f3660

File tree

4 files changed

+138
-7
lines changed

4 files changed

+138
-7
lines changed

__snapshots__/compas-session.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# `compas-session`
2+
3+
## `Dialog when almost expired`
4+
5+
#### `looks like the latest snapshot`
6+
7+
```html
8+
<mwc-dialog
9+
heading="[compas.session.headingExpiring]"
10+
id="compasSessionExpiringDialog"
11+
scrimclickaction=""
12+
>
13+
<div>
14+
[compas.session.explainExpiring]
15+
</div>
16+
<mwc-button
17+
dialogaction="close"
18+
slot="primaryAction"
19+
>
20+
[compas.session.continue]
21+
</mwc-button>
22+
</mwc-dialog>
23+
24+
```
25+
26+
## `Dialog when expired without document`
27+
28+
#### `looks like the latest snapshot`
29+
30+
```html
31+
<mwc-dialog
32+
"=""
33+
escapekeyaction=""
34+
heading="[compas.session.headingExpired]"
35+
id="compasSessionExpiredDialog"
36+
scrimclickaction=""
37+
>
38+
<div>
39+
[compas.session.explainExpiredWithoutProject]
40+
</div>
41+
</mwc-dialog>
42+
43+
```
44+
45+
## `Dialog when expired with document`
46+
47+
#### `looks like the latest snapshot`
48+
49+
```html
50+
<mwc-dialog
51+
"=""
52+
escapekeyaction=""
53+
heading="[compas.session.headingExpired]"
54+
id="compasSessionExpiredDialog"
55+
scrimclickaction=""
56+
>
57+
<div>
58+
[compas.session.explainExpiredWithProject]
59+
</div>
60+
<mwc-button slot="primaryAction">
61+
[compas.session.saveProject]
62+
</mwc-button>
63+
</mwc-dialog>
64+
65+
```
66+

__snapshots__/open-scd.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,10 @@
12031203
Save
12041204
</mwc-button>
12051205
</mwc-dialog>
1206+
<compas-session-expiring-dialog>
1207+
</compas-session-expiring-dialog>
1208+
<compas-session-expired-dialog>
1209+
</compas-session-expired-dialog>
12061210

12071211
```
12081212

src/compas/CompasSession.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import {Dialog} from "@material/mwc-dialog";
44

55
import {saveDocumentToFile} from "../file.js";
66
import {getOpenScdElement} from "./foundation.js";
7-
import {CompasUserInfoService} from "../compas-services/CompasUserInfoService";
7+
8+
import {CompasUserInfoService} from "../compas-services/CompasUserInfoService.js";
89

910
@customElement('compas-session-expiring-dialog')
1011
export class CompasSessionExpiringDialogElement extends LitElement {
@@ -151,19 +152,17 @@ export function renderCompasSessionDialogs(doc: Document | null, docName: string
151152
}
152153

153154
let pingTimer: NodeJS.Timeout | null = null;
154-
let pingScheduled = false;
155155

156156
async function executeKeepAlivePing() {
157157
await CompasUserInfoService().ping()
158-
.finally(() => pingScheduled = false)
158+
.finally(() => pingTimer = null)
159159
}
160160

161161
function schedulePing() {
162-
if (!pingTimer || !pingScheduled) {
162+
if (!pingTimer) {
163163
// Every minute we will send a Ping to the CoMPAS Services while the user is still active.
164164
// This to keep the connection alive so long the user is working.
165165
pingTimer = setTimeout(executeKeepAlivePing, (60 * 1000));
166-
pingScheduled = true;
167166
}
168167
}
169168

@@ -177,7 +176,7 @@ function showExpiredSessionMessage() {
177176
unregisterEvents();
178177
}
179178

180-
function resetTimer() {
179+
export function resetTimer() {
181180
CompasSessionExpiringDialogElement.getElement().resetTimer();
182181
CompasSessionExpiredDialogElement.getElement().resetTimer();
183182
schedulePing();
@@ -191,7 +190,6 @@ function unregisterEvents() {
191190
function registerEvents() {
192191
window.addEventListener('click', resetTimer);
193192
window.addEventListener('keydown', resetTimer);
194-
resetTimer();
195193
}
196194

197195
export function setSessionTimeouts(sessionWarning: number, sessionExpires: number): void {
@@ -201,5 +199,6 @@ export function setSessionTimeouts(sessionWarning: number, sessionExpires: numbe
201199
CompasSessionExpiringDialogElement.getElement().expiringSessionWarning = expiringSessionWarning;
202200
CompasSessionExpiringDialogElement.getElement().expiredSessionMessage = expiredSessionMessage;
203201
CompasSessionExpiredDialogElement.getElement().expiredSessionMessage = expiredSessionMessage;
202+
resetTimer();
204203
registerEvents();
205204
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import {expect, fixture, html} from "@open-wc/testing";
2+
3+
import {
4+
CompasSessionExpiredDialogElement,
5+
CompasSessionExpiringDialogElement,
6+
resetTimer,
7+
setSessionTimeouts
8+
} from "../../../src/compas/CompasSession.js";
9+
10+
import "../../../src/compas/CompasSession.js";
11+
12+
describe('compas-session', () => {
13+
describe('Dialog when almost expired', () => {
14+
let element: CompasSessionExpiringDialogElement;
15+
16+
beforeEach(async () => {
17+
element = await fixture(
18+
html`
19+
<compas-session-expiring-dialog></compas-session-expiring-dialog>`
20+
);
21+
});
22+
23+
it('looks like the latest snapshot', () => {
24+
expect(element).shadowDom
25+
.to.equalSnapshot();
26+
});
27+
});
28+
29+
describe('Dialog when expired without document', () => {
30+
let element: CompasSessionExpiredDialogElement;
31+
32+
beforeEach(async () => {
33+
element = await fixture(
34+
html`
35+
<compas-session-expired-dialog></compas-session-expired-dialog>`
36+
);
37+
});
38+
39+
it('looks like the latest snapshot', () => {
40+
expect(element).shadowDom
41+
.to.equalSnapshot();
42+
});
43+
});
44+
45+
describe('Dialog when expired with document', () => {
46+
const docName = 'station123.scd';
47+
const doc = new Document();
48+
let element: CompasSessionExpiredDialogElement;
49+
50+
beforeEach(async () => {
51+
element = await fixture(
52+
html`
53+
<compas-session-expired-dialog .doc="${doc}" .docName="${docName}"></compas-session-expired-dialog>`
54+
);
55+
});
56+
57+
it('looks like the latest snapshot', () => {
58+
expect(element).shadowDom
59+
.to.equalSnapshot();
60+
});
61+
});
62+
});

0 commit comments

Comments
 (0)