cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <html>
<head>
  <meta charset="utf-8">
  <title>Using expect and deep-freeze</title>
<!--   <script src="https://wzrd.in/standalone/expect@latest"></script>
  <script src="https://wzrd.in/standalone/deep-freeze@latest"></script> -->
</head>
<body>
</body>
</html>
            
          
!
            
              //reducer
const todos = (state = [], action) => {
	//受け取ったアクションのtypeによって切り替わる
	switch (action.type){
		case 'ADD_TODO':
			//stateは配列なので、その中に追加で新しいオブジェクトを与える。
			//与える新しいオブジェクトは、渡されたactionの中の情報を元に生成される
			return [
				...state,
				{
					id: action.id,
					text: action.text,
					completed: false,
				}
			];
		case 'TOGGLE_TODO':
			//mapについて
			//配列であるstateに対して.mapをすると、
			//その中の各配列の値をtodoとして、それに対して =>{}の関数を適用する
			//でreturnされた内容を配列に戻す
      return state.map(todo => {
        if (todo.id !== action.id) {
          return todo;
        }
				
				//todo.completedにtrue or false が入っているので、これをひっくり返す
				//で合成
        return Object.assign({}, todo, {completed: !todo.completed})
      });
	}
};

//deepFreezeはmutateしていないかをチェックするライブラリ
//expectはテスト用のライブラリ
const testAddTodo = () => {
	const stateBefore = [];
	const action = {
		type: 'ADD_TODO',
		id: 0,
		text: 'Test Text',
	};
	
	const stateAfter = [{
		id: 0,
		text: 'Test Text',
		completed: false
	}];
	
	//stateBrforeとactionがmutateしていないかをチェック
	deepFreeze(stateBefore);
	deepFreeze(action);
	
	//todosというファンクションを実行した時に、
	//帰ってくる値が、stateAfterと同じかを.toEqualメソッドで確認
	expect(
		todos(stateBefore, action)
	).toEqual(stateAfter);
};

const testToggleTodo = () => {
	const stateBefore = [
		{
			id: 0,
			text: "Test Text",
			completed: false
		},{
			id: 1,
			text: "Go shopping",
			completed: false
		}
	];
	
	const action = {
		type: 'TOGGLE_TODO',
		id: 0
	};
	
	const stateAfter = [
		{
			id: 0,
			text: "Test Text",
			completed: true
		},{
			id: 1,
			text: "Go shopping",
			completed: false
		}
	];
	
	deepFreeze(stateBefore);
	deepFreeze(action);
	
	expect(
		todos(stateBefore, action)
	).toEqual(stateAfter);
};

//テストを実行。問題あればコンソールにでる。(クロームのコンソールの方をチェック)
testAddTodo();
testToggleTodo();

console.log('All tests passed');
            
          
!
999px
Loading ..................

Console