@orieljs/testing 0.1.0-alpha.6 → 0.1.0-alpha.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/NOTICE CHANGED
@@ -10,3 +10,7 @@ The Oriel name and logo are not covered by that license; see TRADEMARKS.md
10
10
 
11
11
  @orieljs/support (and @orieljs/framework, which bundles it) includes transliteration data from
12
12
  voku/portable-ascii (https://github.com/voku/portable-ascii), Copyright (C) 2019 Lars Moelleken, MIT License.
13
+
14
+ @orieljs/notifications (and @orieljs/framework, which bundles it) includes the HTML and CSS of
15
+ Laravel's default notification mail (laravel/framework, https://github.com/laravel/framework),
16
+ Copyright (c) Taylor Otwell, MIT License.
package/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # @orieljs/testing
2
+
3
+ Testing for Oriel: a fresh application for every test, HTTP requests against it, database helpers and fakes, as Laravel's testing helpers. Tests run on Vitest. In an app, import it from `@orieljs/framework/testing`; `@orieljs/testing` is also published on its own.
4
+
5
+ ```bash
6
+ npm install @orieljs/framework
7
+ npm install -D vitest
8
+ ```
9
+
10
+ ```ts title="tests/Feature/Podcast.test.ts"
11
+ // @filename: tests/TestCase.ts
12
+ import { TestCase as BaseTestCase } from '@orieljs/framework/testing'
13
+ import { afterEach, beforeEach } from 'vitest'
14
+ export class TestCase extends BaseTestCase { static override hooks = { beforeEach, afterEach } }
15
+ // ---cut---
16
+ import { Queue } from '@orieljs/framework'
17
+ import { RefreshDatabase } from '@orieljs/framework/testing'
18
+ import { test } from 'vitest'
19
+ import { ProcessPodcast } from '../../app/Jobs/ProcessPodcast'
20
+ import { User } from '../../app/Models/User'
21
+ import { TestCase } from '../TestCase'
22
+
23
+ const t = TestCase.boot({ uses: [RefreshDatabase] })
24
+
25
+ test('creating a podcast queues its processing', async () => {
26
+ const queue = Queue.fake()
27
+ const user = await User.create({ name: 'Ada', email: 'ada@example.com', password: 'secret' })
28
+
29
+ ;(await t.actingAs(user).post('/podcasts', { title: 'Episode 1' })).assertRedirect('/podcasts')
30
+ queue.assertPushed(ProcessPodcast)
31
+ })
32
+ ```
33
+
34
+ An app's tests extend `tests/TestCase.ts`: `TestCase.boot()` gives each test a new application, and `RefreshDatabase` gives it a migrated database and undoes its changes. `t.get()`, `t.post()` and the other request methods resolve to a `TestResponse`, with assertions such as `assertOk()`, `assertRedirect()`, `assertJson()` and `assertInertia()`. Imported from `@orieljs/framework`, `Mail.fake()`, `Queue.fake()`, `Bus.fake()`, `Event.fake()`, `Notification.fake()` and `Storage.fake()` swap the real services for fakes that record what the app does.
35
+
36
+ Documentation: https://orieljs.vercel.app/docs/testing
37
+
38
+ Status: early alpha. APIs will change.
39
+
40
+ ## License
41
+
42
+ Oriel is open-source software licensed under the Apache License 2.0 (Apache-2.0; see `LICENSE` and `NOTICE`). The Oriel name and logo are trademarks; see `TRADEMARKS.md`.
43
+
44
+ Website: https://orieljs.vercel.app
package/TRADEMARKS.md ADDED
@@ -0,0 +1,33 @@
1
+ # Oriel trademark policy
2
+
3
+ The Oriel code is open source under the Apache License 2.0. The **Oriel name and logo** are not: the license covers
4
+ the code only (section 6 of the license says so), and the name and logo stay with the project. This page explains
5
+ what you can and can't do with them. The goal is simple: people should always be able to tell what is the real Oriel
6
+ and what isn't.
7
+
8
+ ## You can, without asking
9
+
10
+ - Use, change, ship and sell the code, as the license allows.
11
+ - Say that your app or package is "built with Oriel", "for Oriel" or "compatible with Oriel".
12
+ - Name a package `oriel-<something>` or `<something>-for-oriel` when it works with Oriel, as long as it is clear that
13
+ it is not official (for example, don't publish it under the `@orieljs` npm scope).
14
+ - Use the name and logo to write about Oriel, teach it, or link to it, in articles, talks, videos and courses.
15
+ - Use the unmodified logo to point to the project, as it is (no recolouring, cropping or
16
+ distortion).
17
+
18
+ ## You can't, without written permission
19
+
20
+ - Call a fork, a modified copy or a competing product "Oriel", or anything likely to be confused with it.
21
+ - Use the name or logo as the name, logo or domain of your own product, company or service.
22
+ - Suggest that the Oriel project made, endorses or supports your work when it doesn't.
23
+ - Sell merchandise with the name or logo.
24
+
25
+ ## Forks
26
+
27
+ Forks are welcome. Please give yours a different name and logo, keep the `LICENSE` and `NOTICE` files, and say that
28
+ it is based on Oriel.
29
+
30
+ ## Questions
31
+
32
+ Open an issue at https://github.com/orieljs/framework or email meeeet.dev@gmail.com. If you're unsure whether a use
33
+ is fine, ask; the answer is usually yes.
package/dist/index.d.ts CHANGED
@@ -417,6 +417,11 @@ declare class HttpClient {
417
417
  defaultGuard: string | undefined;
418
418
  /** Session attributes carried from one request to the next (Laravel keeps one session store per test). */
419
419
  session: Record<string, unknown> | undefined;
420
+ /**
421
+ * withSession() and flushSession() since the last started session: applied to the stored session (the one the
422
+ * session cookie points to) once the next request starts it, so they win over what it holds, as in Laravel.
423
+ */
424
+ sessionChanges: Array<(session: Session) => void>;
420
425
  /** from(url): the session's previous URL for the next request only (Laravel sets it once). */
421
426
  previousUrl: string | undefined;
422
427
  /** Requests whose actingAs users reached the guards (the AuthFactory they went through). */
@@ -442,6 +447,10 @@ declare class HttpClient {
442
447
  * request sticks. Reads each actingAs guard back and forgets or replaces its user to match.
443
448
  */
444
449
  protected syncActingAs(auth: AuthFactory): Promise<void>;
450
+ /** Applies the pending withSession()/flushSession() changes once the session has loaded what it stored. */
451
+ protected applyOnStart(session: Session): void;
452
+ /** Applies the pending withSession()/flushSession() changes to the session, once. */
453
+ protected applyPending(session: Session): void;
445
454
  /** The request's (scoped) Session, created now so the carried attributes are in place before StartSession. */
446
455
  protected scopeSession(scope: Container): Session | undefined;
447
456
  buildRequest(method: string, uri: string, options: RequestOptions): Request;
@@ -538,9 +547,9 @@ declare abstract class MakesHttpRequests extends InteractsWithApplication {
538
547
  followingRedirects(): this;
539
548
  /** Sets the Referer and the session's previous URL, so redirect()->back() goes there. */
540
549
  from(url: string): this;
541
- /** Puts data in the session for the following requests (Laravel's withSession()). */
550
+ /** Puts data in the session for the following requests, over what the stored session holds (Laravel's withSession()). */
542
551
  withSession(data: Record<string, unknown>): this;
543
- /** Empties the session carried between requests. */
552
+ /** Empties the session, the stored one included, so the next request starts with none (Laravel's flushSession()). */
544
553
  flushSession(): this;
545
554
  /** Every following request is authenticated as `user` on `guard` (which also becomes the default guard). */
546
555
  actingAs(user: Authenticatable, guard?: string): this;
package/dist/index.js CHANGED
@@ -401,19 +401,6 @@ var DatabaseTransactions = {
401
401
  // Concerns/MakesHttpRequests.ts
402
402
  import { AuthFactory as AuthFactory2, token } from "@orieljs/contracts";
403
403
 
404
- // Http/HttpClient.ts
405
- import {
406
- AuthFactory,
407
- ConfigRepository as ConfigRepository3,
408
- Encrypter as Encrypter2,
409
- ExceptionHandler,
410
- HttpKernel,
411
- HttpRequest,
412
- Session,
413
- ViewFactory
414
- } from "@orieljs/contracts";
415
- import { IncomingRequest } from "@orieljs/http";
416
-
417
404
  // environment.ts
418
405
  var TESTING_ENVIRONMENT = Object.freeze({
419
406
  APP_ENV: "testing",
@@ -438,6 +425,19 @@ function clonePlain(value) {
438
425
  return value;
439
426
  }
440
427
 
428
+ // Http/HttpClient.ts
429
+ import {
430
+ AuthFactory,
431
+ ConfigRepository as ConfigRepository3,
432
+ Encrypter as Encrypter2,
433
+ ExceptionHandler,
434
+ HttpKernel,
435
+ HttpRequest,
436
+ Session,
437
+ ViewFactory
438
+ } from "@orieljs/contracts";
439
+ import { IncomingRequest } from "@orieljs/http";
440
+
441
441
  // TestResponse.ts
442
442
  import { Encrypter, UrlGenerator } from "@orieljs/contracts";
443
443
  import { Arr as Arr3 } from "@orieljs/support";
@@ -1549,6 +1549,11 @@ var HttpClient = class {
1549
1549
  defaultGuard;
1550
1550
  /** Session attributes carried from one request to the next (Laravel keeps one session store per test). */
1551
1551
  session;
1552
+ /**
1553
+ * withSession() and flushSession() since the last started session: applied to the stored session (the one the
1554
+ * session cookie points to) once the next request starts it, so they win over what it holds, as in Laravel.
1555
+ */
1556
+ sessionChanges = [];
1552
1557
  /** from(url): the session's previous URL for the next request only (Laravel sets it once). */
1553
1558
  previousUrl;
1554
1559
  /** Requests whose actingAs users reached the guards (the AuthFactory they went through). */
@@ -1584,6 +1589,7 @@ var HttpClient = class {
1584
1589
  const context = { request, app: this.app, client: this };
1585
1590
  const result = await scope.run(async () => {
1586
1591
  this.beforeRequest(scope, context);
1592
+ if (context.session !== void 0) this.applyPending(context.session);
1587
1593
  scope.instance(HttpRequest, new IncomingRequest(request));
1588
1594
  if (scope.bound(AuthFactory)) {
1589
1595
  try {
@@ -1624,6 +1630,7 @@ var HttpClient = class {
1624
1630
  const session = this.scopeSession(scope);
1625
1631
  if (session !== void 0) {
1626
1632
  if (this.session !== void 0) session.put(clonePlain(this.session));
1633
+ if (this.sessionChanges.length > 0) this.applyOnStart(session);
1627
1634
  if (this.previousUrl !== void 0) session.setPreviousUrl(this.previousUrl);
1628
1635
  context.session = session;
1629
1636
  }
@@ -1656,6 +1663,20 @@ var HttpClient = class {
1656
1663
  else this.users.set(guard, user);
1657
1664
  }
1658
1665
  }
1666
+ /** Applies the pending withSession()/flushSession() changes once the session has loaded what it stored. */
1667
+ applyOnStart(session) {
1668
+ const start = session.start.bind(session);
1669
+ session.start = async () => {
1670
+ await start();
1671
+ this.applyPending(session);
1672
+ };
1673
+ }
1674
+ /** Applies the pending withSession()/flushSession() changes to the session, once. */
1675
+ applyPending(session) {
1676
+ if (this.sessionChanges.length === 0) return;
1677
+ for (const change of this.sessionChanges.splice(0)) change(session);
1678
+ if (!session.has("_token")) session.regenerateToken();
1679
+ }
1659
1680
  /** The request's (scoped) Session, created now so the carried attributes are in place before StartSession. */
1660
1681
  scopeSession(scope) {
1661
1682
  if (!scope.bound(Session)) return void 0;
@@ -1930,14 +1951,17 @@ var MakesHttpRequests = class extends InteractsWithApplication {
1930
1951
  this.http.previousUrl = url;
1931
1952
  return this.withHeader("Referer", url);
1932
1953
  }
1933
- /** Puts data in the session for the following requests (Laravel's withSession()). */
1954
+ /** Puts data in the session for the following requests, over what the stored session holds (Laravel's withSession()). */
1934
1955
  withSession(data) {
1935
- this.http.session = { ...this.http.session, ...data };
1956
+ const values = clonePlain(data);
1957
+ this.http.session = { ...this.http.session, ...values };
1958
+ this.http.sessionChanges.push((session) => session.put(clonePlain(values)));
1936
1959
  return this;
1937
1960
  }
1938
- /** Empties the session carried between requests. */
1961
+ /** Empties the session, the stored one included, so the next request starts with none (Laravel's flushSession()). */
1939
1962
  flushSession() {
1940
1963
  this.http.session = {};
1964
+ this.http.sessionChanges.splice(0, Infinity, (session) => session.flush());
1941
1965
  return this;
1942
1966
  }
1943
1967
  /** Every following request is authenticated as `user` on `guard` (which also becomes the default guard). */