-
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from marp-team/auto-reloading
Support HTML auto reloading on watch mode
- Loading branch information
Showing
17 changed files
with
422 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const watcher = require.requireActual('../watcher') | ||
|
||
watcher.notifier.start = jest.fn() | ||
watcher.notifier.sendTo = jest.fn() | ||
|
||
export = watcher |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,5 @@ html(lang=lang) | |
body | ||
!= html | ||
!= readyScript | ||
if watchJs | ||
script!= watchJs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import watch from './watch/watch' | ||
|
||
watch() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export default function() { | ||
const wsPath = (<any>window).__marpCliWatchWS | ||
|
||
if (wsPath) { | ||
const ws = new WebSocket(wsPath) | ||
ws.addEventListener('message', e => { | ||
if (e.data === 'reload') location.reload() | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** @jest-environment jsdom */ | ||
import watch from '../../src/templates/watch/watch' | ||
|
||
const mockAddEventListener = jest.fn() | ||
|
||
beforeEach(() => { | ||
mockAddEventListener.mockReset() | ||
;(<any>window).WebSocket = jest.fn(() => ({ | ||
addEventListener: mockAddEventListener, | ||
})) | ||
}) | ||
|
||
afterEach(() => jest.restoreAllMocks()) | ||
|
||
describe('Watch mode notifier on browser context', () => { | ||
context('when window.__marpCliWatchWS is not defined', () => { | ||
it('does not call WebSocket', () => { | ||
watch() | ||
expect((<any>window).WebSocket).not.toHaveBeenCalled() | ||
}) | ||
}) | ||
|
||
context('when window.__marpCliWatchWS is not defined', () => { | ||
beforeEach(() => | ||
((<any>window).__marpCliWatchWS = 'ws://localhost:52000/test')) | ||
|
||
afterEach(() => delete (<any>window).__marpCliWatchWS) | ||
|
||
it('calls WebSocket', () => { | ||
watch() | ||
expect((<any>window).WebSocket).toHaveBeenCalled() | ||
}) | ||
|
||
it('listens message event', () => { | ||
watch() | ||
expect(mockAddEventListener).toHaveBeenCalledTimes(1) | ||
expect(mockAddEventListener).toHaveBeenCalledWith( | ||
'message', | ||
expect.any(Function) | ||
) | ||
|
||
// Event callback | ||
const reload = jest.spyOn(location, 'reload').mockImplementation() | ||
const [, callback] = mockAddEventListener.mock.calls[0] | ||
|
||
callback({ data: 'ready' }) | ||
expect(reload).not.toHaveBeenCalled() | ||
|
||
callback({ data: 'reload' }) | ||
expect(reload).toHaveBeenCalled() | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.