Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ Vous pouvez également lancer les tests en local avec :
npm run test
```

Les tests sont dans le dossier `tests/` et finissent en `.test.ts`. Les autres
fichiers `.ts` servent à configurer l'environnement de test. En particulier, le
vrai store Baredux est remplacé par un faux store avec des fausses données comme
si on était connecté à Scribouilli et qu'on avait ouvert un site. Ces fausses
données sont dans `tests/fake-store.ts`.

### Note

Pour rendre accessible une nouvelle route :
Expand Down
7 changes: 1 addition & 6 deletions assets/scripts/actions/current-repository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import page from 'page'
import yaml from 'js-yaml'

import store, { type PartialStore } from './../store.ts'
import store from './../store.ts'
import ScribouilliGitRepo, {
makeRepoId,
makePublicRepositoryURL,
Expand Down Expand Up @@ -248,13 +248,8 @@ const getCurrentRepoConfig = (): Promise<any> => {
.catch(handleErrors)
}

const defaultStore = store
export function saveCustomCSS(
css: string,
store: PartialStore<
'currentRepository' | 'gitAgent',
'setTheme'
> = defaultStore,
): ReturnType<typeof file.writeFileAndPushChanges> | Promise<void> {
if (!store.state.currentRepository || !store.state.gitAgent) {
return Promise.resolve()
Expand Down
16 changes: 6 additions & 10 deletions assets/scripts/actions/file.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import GitAgent from '../GitAgent.ts'
import store, { type PartialStore } from '../store.ts'
import store from '../store.ts'

export const writeFileAndCommit = async (
fileName: string,
content: string | Uint8Array,
commitMessage?: string,
localStore: PartialStore<'gitAgent', never> = store,
): Promise<string> => {
if (typeof commitMessage !== 'string' || commitMessage === '') {
commitMessage = `Modification du fichier ${fileName}`
}
const { gitAgent } = localStore.state
const { gitAgent } = store.state

if (!gitAgent) {
throw new TypeError('gitAgent is undefined')
Expand All @@ -24,9 +23,8 @@ export const writeFileAndPushChanges = async (
fileName: string,
content: string | Uint8Array,
commitMessage: string = '',
localStore = store,
): ReturnType<typeof GitAgent.prototype.safePush> => {
const { gitAgent } = localStore.state
const { gitAgent } = store.state

if (!gitAgent) {
throw new TypeError('gitAgent is undefined')
Expand All @@ -39,9 +37,8 @@ export const writeFileAndPushChanges = async (
export const deleteFileAndCommit = async (
fileName: string,
commitMessage: string = '',
localStore = store,
): ReturnType<typeof GitAgent.prototype.commit> => {
const { gitAgent } = localStore.state
const { gitAgent } = store.state

if (!gitAgent) {
throw new TypeError('gitAgent is undefined')
Expand All @@ -58,15 +55,14 @@ export const deleteFileAndCommit = async (
export const deleteFileAndPushChanges = (
fileName: string,
commitMessage: string,
localStore = store,
): ReturnType<typeof GitAgent.prototype.safePush> => {
const { gitAgent } = localStore.state
const { gitAgent } = store.state

if (!gitAgent) {
throw new TypeError('gitAgent is undefined')
}

deleteFileAndCommit(fileName, commitMessage, localStore)
deleteFileAndCommit(fileName, commitMessage)
return gitAgent.safePush()
}

Expand Down
15 changes: 2 additions & 13 deletions assets/scripts/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import GitAgent from './GitAgent.ts'
import ScribouilliGitRepo from './scribouilliGitRepo.ts'
import type { Page, Article } from './types/atelier.ts'
import type { BuildStatus } from './types/git.ts'

/**
* Un store baredux a pour vocation de refléter notamment le modèle mental de la
* personne face à Scribouilli. Le store stocke donc principalement des données (et parfois des singletons)
Expand Down Expand Up @@ -62,7 +63,7 @@ const state: ScribouilliState = {
},
}

const mutations = {
export const mutations = {
setOAuthProvider(
state: ScribouilliState,
oAuthProvider: ScribouilliState['oAuthProvider'],
Expand Down Expand Up @@ -177,16 +178,4 @@ const store: BareduxStore<ScribouilliState, ScribouilliMutations> = Store({
mutations,
})

type PartialMutations<State, Mutations extends keyof ScribouilliMutations> = {
[mutation in Mutations]: (state: State, ...others: any[]) => void | State
}

export type PartialStore<
S extends keyof ScribouilliState = keyof ScribouilliState,
M extends keyof ScribouilliMutations = keyof ScribouilliMutations,
> = BareduxStore<
Pick<ScribouilliState, S>,
PartialMutations<Pick<ScribouilliState, S>, M>
>

export default store
4 changes: 3 additions & 1 deletion assets/scripts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ export const logMessage = (
export const delay = (ms: number): Promise<void> =>
new Promise(resolve => setTimeout(resolve, ms))

export const isItStillCompiling = (lastCommit: CommitObject): boolean => {
export const isItStillCompiling = (
lastCommit: Pick<CommitObject, 'committer'>,
): boolean => {
// Delay (in seconds) after which a non-updated website is assumed to have failed to build.
const ERROR_DELAY = 60 * 1000
const currentTime = new Date().getTime() / 1000
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"ts": "tsc && echo 'Tout va bien 🎉'",
"dev": "rollup -c -w",
"build": "rollup -c",
"test": "mocha --recursive tests --extension ts",
"test": "mocha --recursive tests --extension ts --node-option import=./tests/hook.ts",
"prepare": "husky install",
"svelte-check": "svelte-check --tsconfig tsconfig.json"
},
Expand Down
28 changes: 5 additions & 23 deletions tests/actions/current-repository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,18 @@ import { CUSTOM_CSS_PATH } from '../../assets/scripts/config.ts'
import { describe } from 'mocha'
import sinon from 'sinon'
import { expect } from 'chai'
import GitAgent from '../../assets/scripts/GitAgent.ts'

describe('actions/current-repository.ts', () => {
beforeEach(() => {
sinon.reset()
})

describe('saveCustomCSS', () => {
it('calls writeFile, commit and safePush', done => {
const fakeStore = {
mutations: {
setTheme: sinon.stub(),
},
state: {
currentRepository: {
origin: 'github.com',
publicRepositoryURL: 'https://github.com/test/site',
owner: 'test',
repoName: 'site',
repoId: 'test-site',
publishedWebsiteURL: Promise.resolve('https://test.github.io/site'),
},
gitAgent: new GitAgent({
auth: {},
remoteURL: 'https://github.com',
repoId: 'test-site',
}),
},
subscribe: sinon.stub(),
}
sinon.stub(file, 'writeFileAndPushChanges')
const css = 'body { background: pink; }'

saveCustomCSS(css, fakeStore)
saveCustomCSS(css)

expect(file.writeFileAndPushChanges).to.have.been.calledWithExactly(
CUSTOM_CSS_PATH,
Expand Down
80 changes: 13 additions & 67 deletions tests/actions/file.test.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,29 @@
import './../setup.ts'
import { fakeStateWithOneSite } from './../fixtures.ts'
import {
writeFileAndCommit,
writeFileAndPushChanges,
deleteFileAndCommit,
deleteFileAndPushChanges,
} from './../../assets/scripts/actions/file.ts'
import gitAgent from './../../assets/scripts/GitAgent.ts'
import store from './../../assets/scripts/store.ts'
import sinon from 'sinon'
import { expect } from 'chai'

// Use a common sandbox for all tests so we can easily restore it after each test.
const sandbox = sinon.createSandbox()

describe('actions/file.ts', () => {
beforeEach(() => {
sandbox.stub(store, 'state').value(fakeStateWithOneSite)
})

afterEach(() => {
sandbox.restore()
sinon.reset()
})

describe('writeFileAndCommit', () => {
it('calls writeFile and commit', done => {
const fakeStore = {
state: {
gitAgent: {
writeFile: sinon.stub(),
commit: sinon.stub(),
},
},
mutations: {},
}
writeFileAndCommit(
'test.ts',
'Curiouser and curiouser!',
undefined,
fakeStore,
)
writeFileAndCommit('test.ts', 'Curiouser and curiouser!', undefined)
done()
expect(fakeStore.state.gitAgent.writeFile).to.have.been.calledWith(
expect(store.state.gitAgent!.writeFile).to.have.been.calledWith(
'bla',
'test.ts',
'Curiouser and curiouser!',
)
expect(fakeStore.state.gitAgent.commit).to.have.been.calledWith(
expect(store.state.gitAgent!.commit).to.have.been.calledWith(
'fanne',
'Modification du fichier test.ts',
)
Expand All @@ -55,67 +32,36 @@ describe('actions/file.ts', () => {

describe('writeFileAndPushChanges', () => {
it('calls writeFile, commit and push', done => {
const fakeStore = {
state: {
gitAgent: {
removeFile: sinon.stub(),
commit: sinon.stub(),
},
},
}
writeFileAndPushChanges(
'test.ts',
'Curiouser and curiouser!',
undefined,
fakeStore,
)
writeFileAndPushChanges('test.ts', 'Curiouser and curiouser!')
done()
expect(gitAgent.writeFile).to.have.been.calledWith(
expect(store.state.gitAgent!.writeFile).to.have.been.calledWith(
store.state.currentRepository,
'test.ts',
'Curiouser and curiouser!',
)
expect(gitAgent.commit).to.have.been.calledWith(
expect(store.state.gitAgent!.commit).to.have.been.calledWith(
store.state.currentRepository,
`Modification du fichier test.js`,
)
expect(gitAgent.safePush).to.have.been.calledWith(
expect(store.state.gitAgent!.safePush).to.have.been.calledWith(
store.state.currentRepository,
)
})
})

describe('deleteFileAndCommit', () => {
it('calls removeFile and commit', done => {
const fakeStore = {
state: {
gitAgent: {
removeFile: sinon.stub(),
commit: sinon.stub(),
},
},
}

deleteFileAndCommit('test.ts', undefined, fakeStore)
deleteFileAndCommit('test.ts')
done()
expect(fakeStore.state.gitAgent.removeFile).to.have.been.calledOnce
expect(fakeStore.state.gitAgent.commit).to.have.been.calledOnce
expect(store.state.gitAgent!.removeFile).to.have.been.calledOnce
expect(store.state.gitAgent!.commit).to.have.been.calledOnce
})
})

describe('deleteFileAndPushChanges', () => {
it('calls removeFile and commit', done => {
const fakeStore = {
state: {
gitAgent: {
removeFile: sinon.stub(),
safePush: sinon.stub(),
commit: sinon.stub(),
},
},
}
deleteFileAndPushChanges('test.ts', undefined, fakeStore)
expect(fakeStore.state.gitAgent.safePush).to.have.been.calledOnce
deleteFileAndPushChanges('test.ts', 'Suppression de test.ts')
expect(store.state.gitAgent!.safePush).to.have.been.calledOnce
done()
})
})
Expand Down
Loading